From 439b7750d4750722a03d60465da2e4b62bbcf626 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 11 Dec 2011 12:51:57 +0000 Subject: [PATCH] fixed "hibernateManagedSession" mode to actually work against Hibernate 4.0 (SPR-8776) --- .../hibernate4/HibernateTransactionManager.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 93f362367c..28983de9e5 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 @@ -16,6 +16,7 @@ package org.springframework.orm.hibernate4; +import java.lang.reflect.Method; import java.sql.Connection; import javax.sql.DataSource; @@ -44,6 +45,8 @@ import org.springframework.transaction.support.AbstractPlatformTransactionManage import org.springframework.transaction.support.DefaultTransactionStatus; import org.springframework.transaction.support.ResourceTransactionManager; import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; /** * {@link org.springframework.transaction.PlatformTransactionManager} @@ -102,6 +105,15 @@ import org.springframework.transaction.support.TransactionSynchronizationManager public class HibernateTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean { + /** + * A Method handle for the SessionFactory.getCurrentSession() 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 getCurrentSessionMethod = + ClassUtils.getMethod(SessionFactory.class, "getCurrentSession"); + + private SessionFactory sessionFactory; private DataSource dataSource; @@ -281,7 +293,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana } else if (this.hibernateManagedSession) { try { - Session session = getSessionFactory().getCurrentSession(); + Session session = (Session) ReflectionUtils.invokeMethod(getCurrentSessionMethod, this.sessionFactory); if (logger.isDebugEnabled()) { logger.debug("Found Hibernate-managed Session [" + session + "] for Spring-managed transaction"); }