spring 选择的事务策略需要访问 JTA TransactionManager 或 Unable to build EntityManagerFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4212075/
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
The chosen transaction strategy requires access to the JTA TransactionManager or Unable to build EntityManagerFactory
提问by unknown
I am using Spring 2.0.6 and Hibernate 3.2.x on apache tomcat5.5,now we planed to changed our hybernate mapping files into hybernate+jpa support mapping files. for that we created files like this
我在 apache tomcat5.5 上使用 Spring 2.0.6 和 Hibernate 3.2.x,现在我们计划将我们的 hybernate 映射文件更改为 hybernate+jpa 支持映射文件。为此,我们创建了这样的文件
daoConfig.xml
配置文件
<beans:bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<beans: property name="jndiName">
<beans: value>java:/comp/jdbc/Paymentsdb</beans:value>
</beans: property>
</beans: bean>
<beans: bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<beans: property name="useTransactionAwareDataSource"
value="true" />
<beans: property name="dataSource">
<beans:ref bean="dataSource"/>
</beans: property>
<beans: property name="hibernateProperties">
<beans: props>
<beans:prop key="hibernate.dialect">${database.target}</beans:prop>
<beans:prop key="hibernate.connection.isolation">3</beans:prop>
<beans:prop key="hibernate.current_session_context_class">jta</beans:prop>
<beans:prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
</beans:prop>
<beans: prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</beans: prop>
<beans: prop key="hibernate.connection.release_mode">on_close</beans: prop>
<beans: prop key="hibernate.show_sql">false</beans: prop>
</beans: props>
</beans: property>
</beans: bean>
<beans: bean id="jpaTemplate"
class="org.springframework.orm.jpa.JpaTemplate">
<beans: property name="entityManagerFactory">
<beans:ref bean="entityManagerFactory" />
</beans: property>
</beans: bean>
<beans: bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans: property name="dataSource">
<beans:ref bean="dataSource" />
</beans: property>
<beans: property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans: property name="generateDdl" value="false"/>
<beans: property name="showSql" value="true" />
<beans: property name="databasePlatform" value="${database.target}" />
</beans: bean>
</beans: property>
<beans: property name="persistenceXmlLocation">
<beans:value>classpath:META-INF/persistence.xml</beans: value>
</beans: property>
</beans: bean>
<beans:bean id="atomikosTransactionManager"
class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
destroy-method="close">
<beans:property name="forceShutdown" value="true" />
<beans:property name="startupTransactionService" value="true" />
</beans:bean>
<beans:bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<beans:property name="transactionTimeout">
<beans:value>300</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<beans:property name="transactionManager">
<beans:ref bean="atomikosTransactionManager" />
</beans:property>
<beans:property name="userTransaction">
<beans:ref bean="atomikosUserTransaction" />
</beans:property>
</beans:bean>
</beans: beans>
in my daoconfig.xml i given path for persistence.xml and this is my persistence.xml file
在我的 daoconfig.xml 中,我给出了 persistence.xml 的路径,这是我的 persistence.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="1.0">
<persistence-unit name="payhub" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>com.evolvus.common.model.TaUser</class>
----------
----------
----------
<class>com.evolvus.common.model.TaBooks</class>
</persistence-unit>
</persistence>
here i am specifying my orm mapping file name
orm.xml file is
在这里我指定我的 orm 映射文件名
orm.xml 文件是
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<description>Welcome</description>
<package>com.evolvus.common.model</package>
<!-- TaUser starts-->
<entity class="com.evolvus.common.model.TaUser" name="TaUser">
<table name="ta_user" />
<attributes>
<id name="userId">
<column name="USER_ID"/>
<generated-value strategy="TABLE" />
</id>
<basic name="userName">
<column name="USER_NAME" length="50" />
</basic>
<basic name="password">
<column name="PASS_WORD" length="50" />
</basic>
</attributes>
</entity>
<entity class="com.evolvus.common.model.TaBooks" name="TaBooks">
<table name="ta_user"/>
<attributes>
--------------
--------------
------------
</attributes>
</entity>
</entity-mappings>
when i am trying to run my server .i Am getting the error
当我尝试运行我的服务器时。我收到错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaTemplate' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
... 42 more
give me any suggestions
给我任何建议
采纳答案by Henning
First of all, you're creating both a Hibernate SessionFactory and a JPA EntityManagerFactory. That does not make very much sense unless you have a very odd setup, and you should very likely throw out the SessionFactory configuration. You can either configure plain vanilla Hibernate, or you can configure JPA, you don't need both.
首先,您要创建 Hibernate SessionFactory 和 JPA EntityManagerFactory。除非您的设置非常奇怪,否则这没有多大意义,并且您很可能应该丢弃 SessionFactory 配置。您可以配置普通的 Hibernate,也可以配置 JPA,两者都不需要。
The creation of the entity manager factory fails because you specified JTAas the transaction type, but the JTA transaction manager lookup class is not defined. (You defined it in the Hibernate SessionFactory config, but not in your JPA config.)
实体管理器工厂的创建失败,因为您指定JTA为事务类型,但未定义 JTA 事务管理器查找类。(您在 Hibernate SessionFactory 配置中定义了它,但未在您的 JPA 配置中定义。)
Add this to your persistence.xml:
将此添加到您的persistence.xml:
<persistence-unit name="payhub" transaction-type="JTA">
...
<properties>
<property name="hibernate.transaction.manager_lookup_class"
value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
</properties>
</persistence-unit>
回答by RicardoS
For those who are using Jboss AS, this configuration could be used:
对于使用 Jboss AS 的用户,可以使用此配置:
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
Solution extracted from: http://www.gregbugaj.com/?p=172
解决方案摘自:http: //www.gregbugaj.com/?p=172

