diff --git a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java index 32d34d92c8c..db0524c25cb 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java @@ -605,6 +605,28 @@ public abstract class ClassUtils { return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); } + /** + * Determine whether the given class has a method with the given signature, + * and return it if available (else throws an IllegalStateException). + *

Essentially translates NoSuchMethodException to IllegalStateException. + * @param clazz the clazz to analyze + * @param methodName the name of the method + * @param paramTypes the parameter types of the method + * @return the method (never null) + * @throws IllegalStateException if the method has not been found + * @see java.lang.Class#getMethod + */ + public static Method getMethod(Class clazz, String methodName, Class... paramTypes) { + Assert.notNull(clazz, "Class must not be null"); + Assert.notNull(methodName, "Method name must not be null"); + try { + return clazz.getMethod(methodName, paramTypes); + } + catch (NoSuchMethodException ex) { + throw new IllegalStateException("Expected method not found: " + ex); + } + } + /** * Determine whether the given class has a method with the given signature, * and return it if available (else return null). diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java index 18823000cce..93f362367c5 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java @@ -325,7 +325,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana try { if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) { - Session newSession = getSessionFactory().openSession(); + Session newSession = SessionFactoryUtils.openSession(getSessionFactory()); if (logger.isDebugEnabled()) { logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction"); } diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java index 62a05c7ceba..4a94bdcd7a4 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java @@ -65,10 +65,10 @@ public class LocalSessionFactoryBuilder extends Configuration { new AnnotationTypeFilter(MappedSuperclass.class, false)}; private static final Method addAnnotatedClassMethod = - ClassUtils.getMethodIfAvailable(Configuration.class, "addAnnotatedClass", Class.class); + ClassUtils.getMethod(Configuration.class, "addAnnotatedClass", Class.class); private static final Method addPackageMethod = - ClassUtils.getMethodIfAvailable(Configuration.class, "addPackage", String.class); + ClassUtils.getMethod(Configuration.class, "addPackage", String.class); private final ResourcePatternResolver resourcePatternResolver; diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java index 6c8d9e7a83b..0cdffdc2c54 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java @@ -16,6 +16,7 @@ package org.springframework.orm.hibernate4; +import java.lang.reflect.Method; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -50,6 +51,8 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessResourceUsageException; import org.springframework.jdbc.datasource.DataSourceUtils; +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; /** * Helper class featuring methods for Hibernate Session handling. @@ -74,6 +77,13 @@ public abstract class SessionFactoryUtils { public static final int SESSION_SYNCHRONIZATION_ORDER = DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100; + /** + * A Method handle for the SessionFactory.openSession() method. + * The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes, + * we have to use reflection here as long as we keep compiling against Hibernate 3.x jars. + */ + private static final Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession"); + static final Log logger = LogFactory.getLog(SessionFactoryUtils.class); @@ -91,6 +101,17 @@ public abstract class SessionFactoryUtils { return null; } + /** + * Obtain a new Session from the given SessionFactory. + *

Bridges between Hibernate signature differences. + * @param sessionFactory the SessionFactory to use + * @return the new Session + * @see org.hibernate.SessionFactory#openSession() + */ + public static Session openSession(SessionFactory sessionFactory) { + return (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory); + } + /** * Perform actual closing of the Hibernate Session, * catching and logging any cleanup exceptions thrown. diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java index 29ec3925ad5..0e76d8c35f4 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java @@ -168,7 +168,7 @@ public class OpenSessionInViewFilter extends OncePerRequestFilter { */ protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { try { - Session session = sessionFactory.openSession(); + Session session = SessionFactoryUtils.openSession(sessionFactory); session.setFlushMode(FlushMode.MANUAL); return session; } diff --git a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java index fd7d76abbc8..1c15bd3b8c0 100644 --- a/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java +++ b/org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java @@ -154,7 +154,7 @@ public class OpenSessionInViewInterceptor implements WebRequestInterceptor { */ protected Session openSession() throws DataAccessResourceFailureException { try { - Session session = getSessionFactory().openSession(); + Session session = SessionFactoryUtils.openSession(getSessionFactory()); session.setFlushMode(FlushMode.MANUAL); return session; }