java 通过 JTA 配置 Spring + Hibernate JPA 事务管理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27013353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:04:15  来源:igfitidea点击:

Configuring Spring + Hibernate JPA Transaction Manager through JTA

javaspringhibernatejpa

提问by Gabriel Sanmartin

I previously had this config for Hibernate using RESOURCE-LOCAL transaction type:

我以前使用 RESOURCE-LOCAL 事务类型为 Hibernate 配置了以下配置:

persistence.xml:

持久性.xml:

<persistence-unit name="myPU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

</persistence-unit>

applicationContext (dataaccess bit):

applicationContext(数据访问位):

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"></bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaAdapter" />
    <property name="persistenceUnitName" value="myPU"/>
    <property name="jpaProperties">
        <props>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
</bean>

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <!-- Are there any other properties required? -->
</bean>

<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

    <property name="showSql" value="true" />
    <property name="generateDdl" value="false" />
</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/CNTXESDB" />
    <property name="lookupOnStartup" value="true" />
    <property name="cache" value="true" />
    <property name="proxyInterface" value="javax.sql.DataSource" />
</bean>

But this kind of transaction seems not to work with Glassfish, so I had to switch to JTA transactions.

但是这种事务似乎不适用于 Glassfish,所以我不得不切换到 JTA 事务。

The problem is -- to get Spring to manage transaction creation (through @Transactional) I need to define a TransactionManagerbean but JtaTransactionManagerincluded in spring-tx does not accept an entityManagerFactory bean, so it does not know where the entityManager is in order to open/close/flush Hibernate session.

问题是——为了让 Spring 管理事务创建(通过@Transactional)我需要定义一个TransactionManagerbean 但JtaTransactionManager包含在 spring-tx 中不接受 entityManagerFactory bean,所以它不知道 entityManager 在哪里才能打开/关闭/刷新休眠会话。

So how can I configure Spring with Hibernate to use JTA transactions?

那么如何配置带有 Hibernate 的 Spring 以使用 JTA 事务呢?

EDIT:turns out you canuse RESOURCE_LOCAL transactions with Glassfish, but somehow you cannot have a persistence.xml file. I renamed this file to my_persistence.xmland configured LocalContainerEntityManagerFactoryBeanlike this:

编辑:原来你可以在 Glassfish 中使用 RESOURCE_LOCAL 事务,但不知何故你不能有一个 persistence.xml 文件。我将此文件重命名为my_persistence.xml并配置LocalContainerEntityManagerFactoryBean如下:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaAdapter" />
        <property name="persistenceUnitName" value="myPU"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/my_persistence.xml" />
        <property name="jpaProperties">
            <props>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
    </bean>

回答by Miguel ángel Fernández

I had a similar problem and finally I solved as you can see in this little demo: https://github.com/miguelangelprogramacion/spring4_jpa_hibernate

我有一个类似的问题,最后我解决了,你可以在这个小演示中看到:https: //github.com/miguelangelprogramacion/spring4_jpa_hibernate

With [1] as a reference, I prefer to use Spring's Transaction Support before JTA.

以 [1] 为参考,我更喜欢在 JTA 之前使用 Spring 的 Transaction Support。

Also, I've used an annotation based approach.

另外,我使用了基于注释的方法。

[1] http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

[1] http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/