2
0
Fork 0

droped dup hibernate only code.

This commit is contained in:
Willem Cazander 2014-08-25 22:30:06 +02:00
parent b961970fc3
commit 0a2398c5c1
4 changed files with 0 additions and 574 deletions

View file

@ -25,11 +25,5 @@
<version>${persistence-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate-annotations.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -1,98 +0,0 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.backends.jpa;
import java.io.Serializable;
import net.forwardfire.vasc.backend.AbstractVascBackend;
import net.forwardfire.vasc.backend.VascBackendException;
import org.hibernate.Session;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2007
*/
abstract public class AbstractHibernateVascBackend<DATA_OBJECT extends Serializable> extends AbstractVascBackend<DATA_OBJECT> {
/**
* Provides a hibernate session which is closed !! when transaction is compleeted.
* @return
*/
abstract Session getHibernateSession();
@Override
public void persist(DATA_OBJECT object) throws VascBackendException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
s.persist(object);
s.getTransaction().commit();
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
@Override
public DATA_OBJECT merge(DATA_OBJECT object) throws VascBackendException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
DATA_OBJECT result = (DATA_OBJECT)s.merge(object);
s.getTransaction().commit();
return result;
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
@Override
public void delete(DATA_OBJECT object) throws VascBackendException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Object newObject = s.merge(object);
s.delete(newObject);
s.getTransaction().commit();
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
}

View file

@ -1,35 +0,0 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.backends.jpa;
import org.hibernate.Session;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2009
*/
public interface HibernateSessionProvider {
Session getHibernateSession();
}

View file

@ -1,435 +0,0 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.backends.jpa;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import net.forwardfire.vasc.backend.DefaultVascBackendResult;
import net.forwardfire.vasc.backend.VascBackendException;
import net.forwardfire.vasc.backend.VascBackendResult;
import net.forwardfire.vasc.backend.VascBackendState;
import net.forwardfire.vasc.backend.VascEntryFieldValue;
import net.forwardfire.vasc.backend.VascEntryRecordCreator;
import net.forwardfire.vasc.backend.data.BeanVascEntryFieldValue;
import net.forwardfire.vasc.backend.data.BeanVascEntryRecordCreator;
import net.forwardfire.vasc.xpql.query.QueryParameterValue;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
/**
* Manages persistance with xpql queries
*
* @author Willem Cazander
* @version 1.0 Mar 17, 2009
*/
public class XpqlHibernateVascBackend<DATA_OBJECT extends Serializable> extends AbstractHibernateVascBackend<DATA_OBJECT> {
private HibernateSessionProvider hibernateSessionProvider = null;
private net.forwardfire.vasc.xpql.query.Query query = null;
private net.forwardfire.vasc.xpql.query.Query queryTotal = null;
private net.forwardfire.vasc.xpql.query.Query queryMoveUp = null;
private net.forwardfire.vasc.xpql.query.Query queryMoveUpDown = null;
private net.forwardfire.vasc.xpql.query.Query queryMoveDown = null;
private net.forwardfire.vasc.xpql.query.Query queryMoveDownUp = null;
private Class<?> resultClass = null;
public XpqlHibernateVascBackend() {
}
/**
* @see net.forwardfire.vasc.backends.jpa.AbstractPersistenceVascBackend#getEntityManager()
*/
@Override
Session getHibernateSession() {
return hibernateSessionProvider.getHibernateSession();
}
@Override
public VascBackendResult<DATA_OBJECT> execute(VascBackendState state) throws VascBackendException {
return new DefaultVascBackendResult<DATA_OBJECT>(executeList(state),fetchTotalExecuteSize(state));
}
private List<DATA_OBJECT> executeList(VascBackendState state) throws VascBackendException {
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
query.setQueryParameter(key, value);
if (queryTotal!=null) {
queryTotal.setQueryParameter(key, value);
}
}
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Query q = s.createQuery(query.toPreparedSQL(query));
List<QueryParameterValue> values = query.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
if (isPageable() && state.getPageSize()>0) {
q.setFirstResult(state.getPageIndex()*state.getPageSize());
q.setMaxResults(state.getPageSize());
}
List<DATA_OBJECT> data = q.list();
s.getTransaction().commit();
return data;
} finally {
if (s!=null) {
//em.close();
}
}
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#provideVascEntryFieldValue()
*/
@Override
public VascEntryFieldValue provideVascEntryFieldValue() {
BeanVascEntryFieldValue result = new BeanVascEntryFieldValue();
return result;
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#provideVascEntryRecordCreator()
*/
@Override
public VascEntryRecordCreator provideVascEntryRecordCreator() {
return new BeanVascEntryRecordCreator(resultClass);
}
/**
* @return the query
*/
public net.forwardfire.vasc.xpql.query.Query getQuery() {
return query;
}
/**
* @param query the query to set
*/
public void setQuery(net.forwardfire.vasc.xpql.query.Query query) {
this.query = query;
}
/**
* @return the queryTotal
*/
public net.forwardfire.vasc.xpql.query.Query getQueryTotal() {
return queryTotal;
}
/**
* @param queryTotal the queryTotal to set
*/
public void setQueryTotal(net.forwardfire.vasc.xpql.query.Query queryTotal) {
this.queryTotal = queryTotal;
}
/**
* @return the resultClass
*/
public Class<?> getResultClass() {
return resultClass;
}
/**
* @param resultClass the resultClass to set
*/
public void setResultClass(Class<?> resultClass) {
this.resultClass = resultClass;
}
/**
* @return the hibernateSessionProvider
*/
public HibernateSessionProvider getHibernateSessionProvider() {
return hibernateSessionProvider;
}
/**
* @param hibernateSessionProvider the hibernateSessionProvider to set
*/
public void setHibernateSessionProvider(HibernateSessionProvider hibernateSessionProvider) {
this.hibernateSessionProvider = hibernateSessionProvider;
}
/**
* @see net.forwardfire.vasc.backend.AbstractVascBackend#isPageable()
*/
@Override
public boolean isPageable() {
if (queryTotal==null) {
return false;
}
return true;
}
private long fetchTotalExecuteSize(VascBackendState state) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Query q = s.createQuery(queryTotal.toPreparedSQL(queryTotal));
List<QueryParameterValue> values = queryTotal.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
Long resultTotal = (Long)q.uniqueResult();
s.getTransaction().commit();
return resultTotal;
} finally {
if (s!=null) {
//em.close();
}
}
}
/**
* @see net.forwardfire.vasc.backend.AbstractVascBackend#doRecordMoveDownById(java.lang.Object)
*/
@Override
public long doRecordMoveDownById(VascBackendState state,Object primaryId) throws VascBackendException {
long result = 0l;
if (queryMoveDown!=null) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDown.setQueryParameter(key, value);
}
// set id
queryMoveDown.setQueryParameter("id", primaryId);
// execute update
Query q = s.createQuery(queryMoveDown.toPreparedSQL(queryMoveDown));
List<QueryParameterValue> values = queryMoveDown.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDownUp.setQueryParameter(key, value);
}
// set id
queryMoveDownUp.setQueryParameter("id", primaryId);
// execute update
q = s.createQuery(queryMoveDownUp.toPreparedSQL(queryMoveDownUp));
values = queryMoveDownUp.getOrderQueryParameterValues();
i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
s.getTransaction().commit();
} finally {
if (s!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see net.forwardfire.vasc.backend.AbstractVascBackend#doRecordMoveUpById(java.lang.Object)
*/
@Override
public long doRecordMoveUpById(VascBackendState state,Object primaryId) throws VascBackendException {
long result = 0l;
if (queryMoveUp!=null) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUp.setQueryParameter(key, value);
}
// set id
queryMoveUp.setQueryParameter("id", primaryId);
// execute update
Query q = s.createQuery(queryMoveUp.toPreparedSQL(queryMoveUp));
List<QueryParameterValue> values = queryMoveUp.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUpDown.setQueryParameter(key, value);
}
// set id
queryMoveUpDown.setQueryParameter("id", primaryId);
// execute update
q = s.createQuery(queryMoveUpDown.toPreparedSQL(queryMoveUpDown));
values = queryMoveUpDown.getOrderQueryParameterValues();
i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
s.getTransaction().commit();
} finally {
if (s!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see net.forwardfire.vasc.backend.AbstractVascBackend#isRecordMoveable()
*/
@Override
public boolean isRecordMoveable() {
return queryMoveUp!=null & queryMoveDown!=null;
}
/**
* @return the queryMoveUp
*/
public net.forwardfire.vasc.xpql.query.Query getQueryMoveUp() {
return queryMoveUp;
}
/**
* @param queryMoveUp the queryMoveUp to set
*/
public void setQueryMoveUp(net.forwardfire.vasc.xpql.query.Query queryMoveUp) {
this.queryMoveUp = queryMoveUp;
}
/**
* @return the queryMoveDown
*/
public net.forwardfire.vasc.xpql.query.Query getQueryMoveDown() {
return queryMoveDown;
}
/**
* @param queryMoveDown the queryMoveDown to set
*/
public void setQueryMoveDown(net.forwardfire.vasc.xpql.query.Query queryMoveDown) {
this.queryMoveDown = queryMoveDown;
}
/**
* @return the queryMoveUpDown
*/
public net.forwardfire.vasc.xpql.query.Query getQueryMoveUpDown() {
return queryMoveUpDown;
}
/**
* @param queryMoveUpDown the queryMoveUpDown to set
*/
public void setQueryMoveUpDown(net.forwardfire.vasc.xpql.query.Query queryMoveUpDown) {
this.queryMoveUpDown = queryMoveUpDown;
}
/**
* @return the queryMoveDownUp
*/
public net.forwardfire.vasc.xpql.query.Query getQueryMoveDownUp() {
return queryMoveDownUp;
}
/**
* @param queryMoveDownUp the queryMoveDownUp to set
*/
public void setQueryMoveDownUp(net.forwardfire.vasc.xpql.query.Query queryMoveDownUp) {
this.queryMoveDownUp = queryMoveDownUp;
}
}