Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simultaneous use of Hibernate and Spring data jpa?

Is it possible to use Spring Data JPA (backed by Hibernate as JPA provider) and directly use Hibernate at the same time?

The problem is that when i use JpaTransactionManager, i'm not able to retrieve current session with org.hibernate.HibernateException: No Session found for current thread. When i switch to HibernateTransaction manager, JPA repositories are not able to commit changes.

Here is the part of my Spring context (with that context i'm not able to use direct Hibernate calls):

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/IPGCONF"/>  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"       p:dataSource-ref="dataSource">     <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean>  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">     <property name="persistenceUnitName" value="entityManagerFactory"/>     <property name="dataSource" ref="dataSource"/>     <property name="jpaVendorAdapter">         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>     </property> </bean>  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">     <property name="entityManagerFactory" ref="entityManagerFactory"/>     <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>  <jpa:repositories base-package="com.satgate"/> 

Example of hibernate repository:

public Collection<Layer> listCurrent(Carrier carrier) {     Criteria query = sessionFactory.getCurrentSession()                     .createCriteria(Layer.class)                     .add(Restrictions.eq("carrier", carrier));     query.createCriteria("bitrate")             .addOrder(Order.desc("bitrate"))             .add(Restrictions.eq("symbolrate", carrier.getSymbolrate()));     return query.list(); } 

Example of Spring data repository definition:

public interface BitrateRepository extends PagingAndSortingRepository<Bitrate, Long> { } 

Software versions:

<org.springframework.version>4.0.0.RELEASE</org.springframework.version> <org.springframework.data.version>1.4.3.RELEASE</org.springframework.data.version> <hibernate.version>4.3.0.Final</hibernate.version> 

So, the question is - is it possible to use in the same transaction (specified by @Transactional annotation) both Spring JPA repositories and direct Hibernate calls and how to achieve that?

like image 255
akashihi Avatar asked Feb 10 '14 07:02

akashihi


People also ask

Can we use spring data JPA and Hibernate together?

You cant actually use both of them in the same application.

Do we need Hibernate with spring data JPA?

Hibernate provides a reference implementation of the Java Persistence API that makes it a great choice as an ORM tool with the benefits of loose coupling. Remember, Spring Data JPA always requires the JPA provider such as Hibernate or Eclipse Link.

Can I use JPA without Hibernate?

No, you cannot perform CRUD operations with JPA alone. As JPA is just a specification, you need the implementation to perform database operations. The implementations are provided by Hibernate, EclipseLink, Ibatis, etc.


1 Answers

You need a single way of configuration you are now configuring both Hibernate and JPA. You should be using JPA for configuration so remove the hibernate setup.

You are using Hibernate4 so you can take advantage of the, not so well known, HibernateJpaSessionFactoryBean of Spring. If you need access to the SessionFactory (which I assume you need).

When applied your configuration will like something like this.

<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">     <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">     <property name="persistenceUnitName" value="entityManagerFactory"/>     <property name="dataSource" ref="dataSource"/>     <property name="jpaVendorAdapter">         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>     </property> </bean> 

I would suggest to only use this as an intermediate solution while you are refactoring your applicaiton to use the plain JPA api. I wouldn't suggest mixing both strategies.

like image 178
M. Deinum Avatar answered Oct 08 '22 10:10

M. Deinum