简体   繁体   English

如何在同一应用程序中一起使用Spring JPA和Hibernate?

[英]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. 我使用Hibernate的5和Spring 5,我们希望我们的应用程序同时使用Hibernate和Spring JPA在一起。

How do we configure the transaction managers for both these things in the applicationContext.xml file and use them in the application? 我们如何在applicationContext.xml文件中为这两项配置事务管理器,并在应用程序中使用它们?

Same thing for the beans like entity managers and sessions? 对于诸如实体管理器和会话之类的bean同样有用吗?

Thanks 谢谢

JPA is specification and hibernate is one of the implementation of JPA. JPA是规范,而休眠是JPA的实现之一。 Not sure what exactly you are asking. 不知道你到底在问什么。 If you are using Hibernate, you will configure session transaction manager. 如果您使用的是Hibernate,则将配置会话事务管理器。

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. Spring JPA是一个标准,并且有许多供应商为其提供实现。 Hibernate is one of them. 休眠是其中之一。 So basically you can simply use JPA instead of mix of both. 因此,基本上,您可以简单地使用JPA而不是两者混合使用。

  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 // JPA可以将其用于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 更多信息: 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,您已经指定了文件夹/路径(packagesToScan),JPA应该在其中查找带注释的bean以将其与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. 但是,休眠5的所有功能也可以通过JPA实现。 Can you be more specific as to why do you need both 您能否更具体地说明为什么同时需要两者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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