Tuesday, April 03, 2012

Spring & JPA over Hibernate: Don't forget SpringSessionContext!

NB:  I'm writing this one down so that I don't forget.

If you're using Spring 3.1.1 and JPA with Hibernate as your JPA provider, you're probably using Spring's LocalContainerEntityManagerFactoryBean with a persistenceProviderClass of org.hibernate.ejb.HibernatePersistence.

I was having trouble getting Hibernate's Session to get hooked up to Spring's JpaTransactionManager.  To fix this, include the following entry in your Hibernate properties:
hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext

It's configured automatically when you're using Spring & pure Hibernate with either LocalSessionFactoryBean or AnnotationSessionFactoryBean, but it doesn't appear to be so when using LocalContainerEntityManagerFactoryBean.

Now, hopefully neither your nor I will forget again.

No comments: