提交 0dfb617d 编写于 作者: J Juergen Hoeller

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

上级 de5a007e
......@@ -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 <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,
* and return it if available (else return <code>null</code>).
......
......@@ -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");
}
......
......@@ -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;
......
......@@ -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 <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);
......@@ -91,6 +101,17 @@ public abstract class SessionFactoryUtils {
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,
* catching and logging any cleanup exceptions thrown.
......
......@@ -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;
}
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册