简体   繁体   中英

How to use Spring JPA and Hibernate together in same application?

I am using Hibernate 5 and Spring 5, and we want our application to use both Hibernate and Spring JPA together.

How do we configure the transaction managers for both these things in the applicationContext.xml file and use them in the application?

Same thing for the beans like entity managers and sessions?

Thanks

JPA is specification and hibernate is one of the implementation of JPA. Not sure what exactly you are asking. If you are using Hibernate, you will configure session transaction manager.

eg

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=${hibernate.dialect}
        </value>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
  1. Spring JPA is a standard and there are vendors providing implementation for it. Hibernate is one of them. So basically you can simply use JPA instead of mix of both.

  2. For transaction manager you can define them like this

// Hibernate

<bean id="txManager"    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

// JPA can use this for da

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
   <property name="entityManagerFactory" ref="myEmf" />
</bean>

More info : https://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

  1. For beans you have specify the folder/path(packagesToScan) where JPA should look for annotated beans to map them with DB.

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource" p:packagesToScan="${jpa.entity.packages}"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="${hibernate.show_sql}"/> </property> <property name="jpaProperties"> <props> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> 

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

However all the functionalities of hibernate 5 can be achieved through JPA as well. Can you be more specific as to why do you need both

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM