gpt4 book ai didi

spring - 如何配置 glassfish 以使用应用程序托管实体管理器?

转载 作者:行者123 更新时间:2023-12-02 03:53:34 25 4
gpt4 key购买 nike

如何配置 glassfish 以使用应用程序托管实体管理器?使用下面给出的配置时,我收到以下错误。

我不想让 glassfish 管理事务,我只想在持久性单元中使用带有 RESOURCE_LOCAL 设置的 spring 管理事务,而不是 JTA

SEVERE: Exception while preparing the app : The persistence-context-ref-name [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean/entityManager] .... resolves to a persistence unit called [spring-jpa] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.

我的配置:

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="spring-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>mysql/mydb</non-jta-data-source>
<properties/>
</persistence-unit>
</persistence>

我的应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<context:component-scan base-package="com.myproject"/>
<jpa:repositories base-package="com.myproject"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource"
//....
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter">
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">-->
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>
</property>
</bean>


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

</beans>

最佳答案

我找到了this forum post ,我引用:

In a Java EE environment, if this element [transaction-type] is not specified, the default is JTA. In a Java SE environment, if this element is not specified, a default of RESOURCE_LOCAL may be assumed.

因此,在您的配置中尝试:

<persistence-unit name="spring-jpa" transaction-type="JTA">

关于spring - 如何配置 glassfish 以使用应用程序托管实体管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13904242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com