Is it possible to use SpringData-JPA with a hibernate4.LocalSessionFactoryBean?
hibernate, spring, spring-data, spring-data-jpa
Solution
Although they can coexists it will be problematic especially if you want to have them participate in the same transaction. However if you switch your logic around and configure a `LocalContainerEntityManagerFactoryBean` instead of a `LocalSessionFactoryBean` you can use the `HibernateJpaSessionFactoryBean` to get access to the underlying `SessionFactory`.
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- Your properties here -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Now you have both and can participate in the same transaction.
This solution is also documented in the Spring Data JPA reference guide in the FAQ section.
Problem
I am already using Hibernate 4 directly with a `LocalSessionFactoryBean` and a `SessionFactory` in my code. I would now like to include Spring-Data-JPA in my code. But Spring-Data needs an `EntityManagerFactory` to work, which can be configured through a `LocalContainerEntityManagerFactoryBean`. Can these Beans `LocalSessionFactoryBean` and `LocalContainerEntityManagerFactoryBean` coexist in one Spring project? (Or can one be adapted by the other?) What is the best practice?