This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

How to use Hibernate and Sring Data JPA at the same time?

Can YOU also use Sring Data JPA (supported by Hibernate as a JPA provider) Hibernate?

The problem is that when I use the JpaTransactionManager. I can’t use the org hibernate. HibernateException retrieve the current session: can’t find the current thread of conversation. When I switched to HibernateTransaction Manager, the JPA repository was unable to commit changes.

Here is part of my Spring context (in which I cannot 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"/>
Copy the code

Hibernate storage code example:

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();
}
Copy the code

Example of Sring Data jPA array storage:

public interface BitrateRepository extends PagingAndSortingRepository<Bitrate.Long> {}Copy the code

Software version

<org.springframework.version>4.0. 0.RELEASE</org.springframework.version>
<org.springframework.data.version>1.43..RELEASE</org.springframework.data.version>
<hibernate.version>4.3. 0.Final</hibernate.version>
Copy the code

So, the question is — is it possible to use both Spring JPA storage and Hibernate calls in the same transaction (specified by @Transactional Annotation), and how?

Answer a

Use EntityManager.unwrap (session.class) to get the Hibernate Session and retrieve the Session factory from the Session object rather than creating it.

You can also use the EntityManagerFactory. Unwrap (SessionFactory. Class) direct access to Hibernate SessionFactory.

Answer two

You need a single way to configure, and you are now configuring Hibernate and JPA. You should use JPA for configuration, so remove hibernate Settings.

You are using a Hibernate4, so can use Spring’s hibernatejpassessionfactorybean, it is not known. If you need access to SessionFactor (I assume you do).

Your configuration might look 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>
Copy the code

I recommend that you refactor your application to use the normal JPA API and only use Hibernate as an intermediate solution. I don’t recommend mixing the two strategies.

The article translated from Stack Overflow:stackoverflow.com/questions/2…