refer to correct openSession() method for Hibernate 4.0 (SPR-8776)

This commit is contained in:
Juergen Hoeller 2011-10-20 10:23:49 +00:00
parent de5a007e46
commit 0dfb617d8a
6 changed files with 48 additions and 5 deletions

View File

@ -605,6 +605,28 @@ public abstract class ClassUtils {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); 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 <code>IllegalStateException</code>).
* <p>Essentially translates <code>NoSuchMethodException</code> to <code>IllegalStateException</code>.
* @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 <code>null</code>)
* @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, * Determine whether the given class has a method with the given signature,
* and return it if available (else return <code>null</code>). * and return it if available (else return <code>null</code>).

View File

@ -325,7 +325,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
try { try {
if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) { if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
Session newSession = getSessionFactory().openSession(); Session newSession = SessionFactoryUtils.openSession(getSessionFactory());
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction"); logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
} }

View File

@ -65,10 +65,10 @@ public class LocalSessionFactoryBuilder extends Configuration {
new AnnotationTypeFilter(MappedSuperclass.class, false)}; new AnnotationTypeFilter(MappedSuperclass.class, false)};
private static final Method addAnnotatedClassMethod = private static final Method addAnnotatedClassMethod =
ClassUtils.getMethodIfAvailable(Configuration.class, "addAnnotatedClass", Class.class); ClassUtils.getMethod(Configuration.class, "addAnnotatedClass", Class.class);
private static final Method addPackageMethod = private static final Method addPackageMethod =
ClassUtils.getMethodIfAvailable(Configuration.class, "addPackage", String.class); ClassUtils.getMethod(Configuration.class, "addPackage", String.class);
private final ResourcePatternResolver resourcePatternResolver; private final ResourcePatternResolver resourcePatternResolver;

View File

@ -16,6 +16,7 @@
package org.springframework.orm.hibernate4; package org.springframework.orm.hibernate4;
import java.lang.reflect.Method;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -50,6 +51,8 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException; import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/** /**
* Helper class featuring methods for Hibernate Session handling. * Helper class featuring methods for Hibernate Session handling.
@ -74,6 +77,13 @@ public abstract class SessionFactoryUtils {
public static final int SESSION_SYNCHRONIZATION_ORDER = public static final int SESSION_SYNCHRONIZATION_ORDER =
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100; DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100;
/**
* A Method handle for the <code>SessionFactory.openSession()</code> 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); static final Log logger = LogFactory.getLog(SessionFactoryUtils.class);
@ -91,6 +101,17 @@ public abstract class SessionFactoryUtils {
return null; return null;
} }
/**
* Obtain a new Session from the given SessionFactory.
* <p>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, * Perform actual closing of the Hibernate Session,
* catching and logging any cleanup exceptions thrown. * catching and logging any cleanup exceptions thrown.

View File

@ -168,7 +168,7 @@ public class OpenSessionInViewFilter extends OncePerRequestFilter {
*/ */
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
try { try {
Session session = sessionFactory.openSession(); Session session = SessionFactoryUtils.openSession(sessionFactory);
session.setFlushMode(FlushMode.MANUAL); session.setFlushMode(FlushMode.MANUAL);
return session; return session;
} }

View File

@ -154,7 +154,7 @@ public class OpenSessionInViewInterceptor implements WebRequestInterceptor {
*/ */
protected Session openSession() throws DataAccessResourceFailureException { protected Session openSession() throws DataAccessResourceFailureException {
try { try {
Session session = getSessionFactory().openSession(); Session session = SessionFactoryUtils.openSession(getSessionFactory());
session.setFlushMode(FlushMode.MANUAL); session.setFlushMode(FlushMode.MANUAL);
return session; return session;
} }