Hi all,
Sorry if this is a FAQ, but I can't seem to google the answer.
Env: Spring 3.1.1.RELEASE, Spring Data JPA 1.1.0.RC1, Hibernate 3.6.10.Final
Motivation: Migrating evolutionarily from pure Hibernate to JPA w/Hibernate as impl. Some components still need SessionFactory & Session instances during migration.
If I'm using a LocalContainerEntityManagerFactoryBean along with Hibernate and I'm NOT using a META-INF/persistence.xml file, what's the correct incantation of Hibernate properties in order to get the following to return non-null?
Code:
public Session getCurrentSession() {
EntityManagerFactory emf = beanFactory.getBean(EntityManagerFactory.class);
SessionFactory sf = ((HibernateEntityManagerFactory)emf).getSessionFactory();
return sf.getCurrentSession(); // returning null! :(
}
In particular, I'm looking for values I have to set for the following:
- hibernate.current_session_context_class
- hibernate.transaction.factory_class
- hibernate.transaction.manager_lookup_class
I can see in my logs, with none of these properties set, that a Session is getting created,
Code:
03/20/12 22:00:12:724: [] [] [SpringOsgiExtenderThread-37]: INFO com.ea.nucleus.eaid.impl.group.GroupServiceImpl - Initializing group service impl.
03/20/12 22:00:12:735: [] [] [SpringOsgiExtenderThread-37]: INFO com.ea.nucleus.eaid.impl.group.GroupServiceImpl - Loading all groups and creating group map.
03/20/12 22:00:12:740: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
03/20/12 22:00:12:740: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
03/20/12 22:00:12:921: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 5457022209167360
03/20/12 22:00:12:921: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 5457022209167360
03/20/12 22:00:12:926: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@5f297ee0] for JPA transaction
03/20/12 22:00:12:926: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@5f297ee0] for JPA transaction
03/20/12 22:00:12:947: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - begin
03/20/12 22:00:12:947: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - begin
03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
03/20/12 22:00:13:030: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565d1ac3]
03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true
03/20/12 22:00:13:028: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit
03/20/12 22:00:13:030: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565d1ac3]
03/20/12 22:00:13:060: [] [] [SpringOsgiExtenderThread-37]: DEBUG org.springframework.transaction.support.TransactionTemplate - Initiating transaction rollback on application exception
org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:468)
but getCurrentSession() is returning null, suggesting to me that I'm missing some plumbing setup between Spring & Hibernate.
What does a dude gotta do to get this to work?
TIA,
Matthew