|
From: <gca...@us...> - 2012-07-05 15:04:08
|
Revision: 4094
http://openutils.svn.sourceforge.net/openutils/?rev=4094&view=rev
Author: gcatania
Date: 2012-07-05 15:03:58 +0000 (Thu, 05 Jul 2012)
Log Message:
-----------
BSHD-16 hibernate 4 compatbility changes
Modified Paths:
--------------
trunk/openutils-bshd5/pom.xml
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
trunk/openutils-bshd5/src/test/resources/hibernate.cfg.xml
trunk/openutils-bshd5/src/test/resources/spring-tests.xml
Modified: trunk/openutils-bshd5/pom.xml
===================================================================
--- trunk/openutils-bshd5/pom.xml 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/pom.xml 2012-07-05 15:03:58 UTC (rev 4094)
@@ -53,7 +53,14 @@
</descriptors>
</configuration>
</plugin>
- <plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <!-- BSHD-16 without manifest only jar, hibernate has classpath issues -->
+ <useManifestOnlyJar>true</useManifestOnlyJar>
+ </configuration>
+ </plugin>
+ <plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
@@ -147,6 +154,7 @@
<version>6.5.2</version>
<scope>test</scope>
</dependency>
+<!--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
@@ -167,10 +175,11 @@
</exclusion>
</exclusions>
</dependency>
+-->
</dependencies>
<properties>
<slf4j.version>1.6.6</slf4j.version>
- <hibernate.version>3.5.6-Final</hibernate.version>
+ <hibernate.version>4.1.1.Final</hibernate.version>
<javassist.version>3.16.1-GA</javassist.version>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
@@ -204,7 +213,7 @@
<hibernate.url>jdbc:derby:memory:daotest;create=true</hibernate.url>
<hibernate.user>sa</hibernate.user>
<hibernate.password></hibernate.password>
- <hibernate.dialect>org.hibernate.dialect.DerbyDialect</hibernate.dialect>
+ <hibernate.dialect>org.hibernate.dialect.DerbyTenSevenDialect</hibernate.dialect>
</properties>
<build>
<plugins>
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2012-07-05 15:03:58 UTC (rev 4094)
@@ -318,7 +318,9 @@
* Load object matching the given key and return it. Lazy object will be initialized.
* @param key the id of the entity instance to load
* @return the found entity instance, or null if none found
+ * @deprecated same as {@link #get(Serializable)}
*/
+ @Deprecated
T loadIfAvailable(K key);
/**
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-07-05 15:03:58 UTC (rev 4094)
@@ -30,7 +30,6 @@
import it.openutils.hibernate.example.FilterMetadataSupport;
import java.io.Serializable;
-import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -42,9 +41,9 @@
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
+import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
@@ -52,9 +51,9 @@
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.type.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
-import org.springframework.orm.hibernate3.HibernateCallback;
-import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
@@ -64,11 +63,13 @@
* @param <T> Persistence class
* @param <K> Object Key
*/
-public abstract class HibernateDAOImpl<T, K extends Serializable> extends HibernateDaoSupport
- implements
- HibernateDAO<T, K>
+public abstract class HibernateDAOImpl<T, K extends Serializable> implements HibernateDAO<T, K>
{
+ private final Logger logger = LoggerFactory.getLogger(getClass());
+
+ private SessionFactory sessionFactory;
+
private Class<T> referenceClass;
private boolean aopenabled;
@@ -106,7 +107,21 @@
this.referenceClass = referenceClass;
}
+ private Session getCurrentSession()
+ {
+ return sessionFactory.getCurrentSession();
+ }
+
/**
+ * creates a criteria for this dao's reference class, associated to the current session
+ * @return a new criteria for the reference class
+ */
+ private Criteria createCriteria()
+ {
+ return getCurrentSession().createCriteria(getReferenceClass());
+ }
+
+ /**
* {@inheritDoc}
*/
public List<T> findAll()
@@ -128,29 +143,22 @@
*/
public List<T> find(final List< ? extends Criterion> criteria, final Order... orders)
{
- return getHibernateTemplate().execute(new HibernateCallback<List<T>>()
+ Criteria crit = createCriteria();
+ if (criteria != null)
{
-
- public List<T> doInHibernate(final Session ses) throws HibernateException
+ for (Criterion c : criteria)
{
- Criteria crit = ses.createCriteria(getReferenceClass());
- if (criteria != null)
- {
- for (Criterion c : criteria)
- {
- crit.add(c);
- }
- }
- if (orders != null)
- {
- for (Order o : orders)
- {
- crit.addOrder(o);
- }
- }
- return crit.list();
+ crit.add(c);
}
- });
+ }
+ if (orders != null)
+ {
+ for (Order o : orders)
+ {
+ crit.addOrder(o);
+ }
+ }
+ return crit.list();
}
/**
@@ -158,7 +166,7 @@
*/
public List<T> find(String query)
{
- return getHibernateTemplate().find(query);
+ return getCurrentSession().createQuery(query).list();
}
/**
@@ -174,15 +182,7 @@
*/
public List<T> find(final String query, final Object[] paramValues, final Type[] paramTypes)
{
- return getHibernateTemplate().execute(new HibernateCallback<List<T>>()
- {
-
- public List<T> doInHibernate(final Session ses) throws HibernateException
- {
- // hibernate 3
- return ses.createQuery(query).setParameters(paramValues, paramTypes).list();
- }
- });
+ return getCurrentSession().createQuery(query).setParameters(paramValues, paramTypes).list();
}
/**
@@ -312,23 +312,18 @@
*/
public T load(K key)
{
- T result = getHibernateTemplate().load(getReferenceClass(), key);
+ T result = (T) sessionFactory.getCurrentSession().load(getReferenceClass(), key);
Hibernate.initialize(result);
return result;
}
/**
- * {@inheritDoc}
+ * @deprecated same as {@link #get(Serializable)};
*/
+ @Deprecated
public T loadIfAvailable(K key)
{
- T result = getHibernateTemplate().get(getReferenceClass(), key);
- if (result != null)
- {
- Hibernate.initialize(result);
- return result;
- }
- return null;
+ return get(key);
}
/**
@@ -336,7 +331,7 @@
*/
public T get(K key)
{
- return getHibernateTemplate().get(getReferenceClass(), key);
+ return (T) sessionFactory.getCurrentSession().get(getReferenceClass(), key);
}
/**
@@ -344,7 +339,7 @@
*/
public K save(T obj)
{
- return (K) getHibernateTemplate().save(obj);
+ return (K) sessionFactory.getCurrentSession().save(obj);
}
/**
@@ -352,7 +347,7 @@
*/
public void update(T obj)
{
- getHibernateTemplate().update(obj);
+ sessionFactory.getCurrentSession().update(obj);
}
/**
@@ -360,7 +355,7 @@
*/
public void saveOrUpdate(T obj)
{
- getHibernateTemplate().saveOrUpdate(obj);
+ sessionFactory.getCurrentSession().saveOrUpdate(obj);
}
/**
@@ -368,15 +363,10 @@
*/
public boolean delete(final K key)
{
- return getHibernateTemplate().execute(new HibernateCallback<Boolean>()
- {
-
- public Boolean doInHibernate(final Session ses) throws HibernateException
- {
- ses.delete(ses.load(getReferenceClass(), key));
- return true;
- }
- });
+ Session s = getCurrentSession();
+ Object toDelete = s.load(getReferenceClass(), key);
+ s.delete(toDelete);
+ return true;
}
/**
@@ -384,7 +374,7 @@
*/
public void refresh(T obj)
{
- getHibernateTemplate().refresh(obj);
+ getCurrentSession().refresh(obj);
}
/**
@@ -392,7 +382,7 @@
*/
public void evict(T obj)
{
- getHibernateTemplate().evict(obj);
+ getCurrentSession().evict(obj);
}
/**
@@ -400,15 +390,7 @@
*/
public T merge(final T obj)
{
- return getHibernateTemplate().execute(new HibernateCallback<T>()
- {
-
- public T doInHibernate(final Session ses) throws HibernateException
- {
- return (T) ses.merge(obj);
- }
- });
-
+ return (T) getCurrentSession().merge(obj);
}
/**
@@ -425,7 +407,7 @@
*/
public List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page, Order... orders)
{
- return getHibernateTemplate().execute(new ExampleTreeCallback<T>(exampleTree, maxResults, page, orders));
+ return new ExampleTreeCallback<T>(exampleTree, maxResults, page, orders).doInHibernate(getCurrentSession());
}
/**
@@ -443,8 +425,8 @@
public List<Object> findFilteredProperties(ExampleTree exampleTree, int maxResults, int page,
List<String> properties, Order... orders)
{
- return getHibernateTemplate().execute(
- new ExampleTreePropertiesCallback(exampleTree, maxResults, page, properties, orders));
+ return new ExampleTreePropertiesCallback(exampleTree, maxResults, page, properties, orders)
+ .doInHibernate(getCurrentSession());
}
/**
@@ -483,7 +465,7 @@
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
List< ? extends Criterion> criteria, Order... orders)
{
- HibernateCallback<List<T>> callback;
+ BaseCallback<T> callback;
if (MapUtils.isEmpty(metadata) && CollectionUtils.isEmpty(criteria))
{
callback = new ExampleTreeCallback<T>(defaultExample(filter), maxResults, page, orders);
@@ -492,7 +474,7 @@
{
callback = new LegacySupportCallback<T>(filter, maxResults, page, metadata, criteria, orders);
}
- return getHibernateTemplate().execute(callback);
+ return callback.doInHibernate(getCurrentSession());
}
/**
@@ -515,7 +497,7 @@
Map<String, ? extends FilterMetadata> metadata, int maxResults, int page, List< ? extends Criterion> criteria,
List<String> properties)
{
- HibernateCallback<List<Object>> callback;
+ BaseCallback<Object> callback;
if (MapUtils.isEmpty(metadata) && CollectionUtils.isEmpty(criteria))
{
callback = new ExampleTreePropertiesCallback(defaultExample(filter), maxResults, page, properties, orders);
@@ -531,7 +513,7 @@
properties,
orders);
}
- return getHibernateTemplate().execute(callback);
+ return callback.doInHibernate(getCurrentSession());
}
/**
@@ -575,26 +557,19 @@
*/
protected List<Object> findByNamedQuery(final String name, final Serializable[] params, final Integer maxResults)
{
- return getHibernateTemplate().execute(new HibernateCallback<List<Object>>()
+ Query q = getCurrentSession().getNamedQuery(name);
+ if (maxResults != null)
{
-
- public List<Object> doInHibernate(final Session ses) throws HibernateException
+ q.setMaxResults(maxResults);
+ }
+ if (params != null)
+ {
+ for (int i = 0; i < params.length; i++)
{
- Query q = ses.getNamedQuery(name);
- if (maxResults != null)
- {
- q.setMaxResults(maxResults);
- }
- if (params != null)
- {
- for (int i = 0; i < params.length; i++)
- {
- q.setParameter(i, params[i]);
- }
- }
- return q.list();
+ q.setParameter(i, params[i]);
}
- });
+ }
+ return q.list();
}
/**
@@ -606,27 +581,20 @@
*/
protected List<Object> findByNamedQuery(final String name, final Map<String, ? > params, final Integer maxResults)
{
- return getHibernateTemplate().execute(new HibernateCallback<List<Object>>()
+ Query q = getCurrentSession().getNamedQuery(name);
+ if (maxResults != null)
{
+ q.setMaxResults(maxResults);
+ }
- public List<Object> doInHibernate(final Session ses) throws HibernateException
+ if (params != null)
+ {
+ for (Map.Entry<String, ? > entry : params.entrySet())
{
- Query q = ses.getNamedQuery(name);
- if (maxResults != null)
- {
- q.setMaxResults(maxResults);
- }
-
- if (params != null)
- {
- for (Map.Entry<String, ? > entry : params.entrySet())
- {
- setParameterValue(q, entry.getKey(), entry.getValue());
- }
- }
- return q.list();
+ setParameterValue(q, entry.getKey(), entry.getValue());
}
- });
+ }
+ return q.list();
}
/**
@@ -743,17 +711,33 @@
}
catch (AspectException exc)
{
- logger.debug("Not running inside an AOP proxy, so no proxy can be returned: " + exc.getMessage());
+ logger.debug("Not running inside an AOP proxy, so no proxy can be returned: {}", exc.getMessage());
}
catch (IllegalStateException e)
{
- logger.warn("Cannot access proxy: " + e.getMessage());
+ logger.warn("Cannot access proxy: {}", e.getMessage());
aopenabled = false;
}
return this;
}
/**
+ * @param sessionFactory the sessionFactory to set
+ */
+ public void setSessionFactory(SessionFactory sessionFactory)
+ {
+ this.sessionFactory = sessionFactory;
+ }
+
+ /**
+ * @return the sessionFactory
+ */
+ public SessionFactory getSessionFactory()
+ {
+ return sessionFactory;
+ }
+
+ /**
* @author gcatania
*/
@SuppressWarnings("deprecation")
@@ -847,7 +831,7 @@
* @author gcatania
* @param R the result class
*/
-abstract class BaseCallback<R> implements HibernateCallback<List<R>>
+abstract class BaseCallback<R>
{
private final Order[] orders;
@@ -870,7 +854,7 @@
*/
protected abstract Criteria createCriteria(Session session);
- public final List<R> doInHibernate(Session session) throws HibernateException, SQLException
+ public final List<R> doInHibernate(Session session)
{
Criteria crit = createCriteria(session);
crit.setMaxResults(maxResults);
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-07-05 15:03:58 UTC (rev 4094)
@@ -37,7 +37,6 @@
import java.util.Set;
import org.hibernate.Criteria;
-import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
@@ -239,12 +238,9 @@
private final SessionFactory sessionFactory;
- private final EntityMode entityMode;
-
public ExampleTreeWalker(Session session)
{
sessionFactory = session.getSessionFactory();
- entityMode = session.getEntityMode();
}
public Criteria walk(Criteria rootCriteria)
@@ -281,7 +277,7 @@
continue;
}
- Object propertyValue = classMetadata.getPropertyValue(entity, propertyName, entityMode);
+ Object propertyValue = classMetadata.getPropertyValue(entity, propertyName);
if (propertyType.isCollectionType())
{
propertyValue = ExampleTreeUtils.getValueFromCollection(propertyValue);
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-07-05 15:03:58 UTC (rev 4094)
@@ -33,7 +33,7 @@
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
-import org.hibernate.engine.SessionImplementor;
+import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.metadata.ClassMetadata;
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-07-05 15:03:58 UTC (rev 4094)
@@ -33,7 +33,6 @@
import java.util.Set;
import org.hibernate.Criteria;
-import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
@@ -96,12 +95,9 @@
private final SessionFactory sessionFactory;
- private final EntityMode entityMode;
-
public ExampleTreeWalker(Session session)
{
sessionFactory = session.getSessionFactory();
- entityMode = session.getEntityMode();
}
public Criteria walk(Criteria rootCriteria, Object rootEntity)
@@ -128,7 +124,7 @@
{
continue;
}
- Object propertyValue = classMetadata.getPropertyValue(entity, propertyName, entityMode);
+ Object propertyValue = classMetadata.getPropertyValue(entity, propertyName);
FilterMetadata fm = currFilterMetadata.get(propertyName);
if (fm != null && propertyValue != null)
{
Modified: trunk/openutils-bshd5/src/test/resources/hibernate.cfg.xml
===================================================================
--- trunk/openutils-bshd5/src/test/resources/hibernate.cfg.xml 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/test/resources/hibernate.cfg.xml 2012-07-05 15:03:58 UTC (rev 4094)
@@ -1,6 +1,6 @@
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+ "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="it.openutils.hibernate.test.model.Address" />
Modified: trunk/openutils-bshd5/src/test/resources/spring-tests.xml
===================================================================
--- trunk/openutils-bshd5/src/test/resources/spring-tests.xml 2012-07-05 14:55:25 UTC (rev 4093)
+++ trunk/openutils-bshd5/src/test/resources/spring-tests.xml 2012-07-05 15:03:58 UTC (rev 4094)
@@ -3,11 +3,11 @@
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+ http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
+ http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="it.openutils.hibernate.test" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
@@ -16,10 +16,9 @@
<property name="username" value="${hibernate.user}" />
<property name="password" value="${hibernate.password}" />
</bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
+ <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
@@ -28,7 +27,7 @@
</props>
</property>
</bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
+ <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|