Hibernate JPA 和 Spring javax.persistence.TransactionRequiredException:没有正在进行的事务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1801828/
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
Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress
提问by kcheng
When I call:
当我打电话时:
entityManager.flush()
I get the exception mentioned in the title.
我得到了标题中提到的例外。
I am using Hibernate JPA.
我正在使用 Hibernate JPA。
回答by Chandra Sekar
Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.
确保在执行此语句时您有一个活动的事务。如果您使用 JPA,请使用 EntityManager.getTransaction().begin()。这是假设您在 JTA 事务范围之外使用 JPA。
If you are running the application inside a container with JTA support you can also use JTA UserTransaction to manage transactions.
如果您在支持 JTA 的容器中运行应用程序,您还可以使用 JTA UserTransaction 来管理事务。
回答by Dapeng
Please make sure that your handler method is declared as public
请确保您的处理程序方法声明为 public
@Transactional
@RequestMapping('/test')
public String doTest() {
// do your stuff here
return 'testview';
}
回答by Maciej Bi?as
Make sure that your spring configuration includes the following line:
确保您的弹簧配置包括以下行:
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
mode
can be either proxyor aspectjand transaction-manager
has to point to your transaction manager been.
mode
可以是proxy或aspectj并且transaction-manager
必须指向您的事务管理器。
回答by Nestor Urquiza
Same was happening to me using spring 3.0.0 / 3.0.3. Data was persisted in MySQL from JUnit but not from the tomcat server. After so much work I gave up on RESOURCE_LOCAL for JTA.
使用 spring 3.0.0 / 3.0.3 也发生在我身上。数据从 JUnit 保存在 MySQL 中,而不是从 tomcat 服务器保存。经过这么多工作,我放弃了 JTA 的 RESOURCE_LOCAL。
This worked for me http://erich.soomsam.net/2007/04/24/spring-jpa-and-jta-with-hibernate-and-jotm/It uses JTA and depends on JOTM.
这对我有用http://erich.soomsam.net/2007/04/24/spring-jpa-and-jta-with-hibernate-and-jotm/它使用 JTA 并依赖于 JOTM。
回答by Jason Gritman
For JBoss 4.0 and Hibernate, I fixed this problem by adding some transaction manager properties to my EntityManagerFactoryBean
definition:
对于 JBoss 4.0 和 Hibernate,我通过在我的EntityManagerFactoryBean
定义中添加一些事务管理器属性来解决这个问题:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="xaDs" />
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup
</prop>
</props>
</property>
I found the soluton on this message board thread.
我在这个留言板上找到了解决方案。
回答by Roman
After encountering this problem myself and spending a few hours trying to get it resolved I finally found a reason for it: Spring has a bug and can't maintain transactions with @Transactional
annotation if the same class has @Service
annotation for the means of autowiring.
在我自己遇到这个问题并花了几个小时试图解决它之后,我终于找到了一个原因:Spring 有一个错误,@Transactional
如果同一个类具有@Service
自动装配方式的注释,则无法使用注释维护事务。
Once the @Service
annotation was removed from the service class in question, and an appropriate bean was declared in the XML config:
一旦@Service
从相关服务类中删除了注释,并在 XML 配置中声明了一个适当的 bean:
<bean id="myService" class="com.example.myapp.service.MyServiceImpl" />
the problem is gone.
问题消失了。
Check this JIRA bugfor more details.
检查此JIRA 错误以获取更多详细信息。
回答by Mannie
My Problem was to do with the way that I setup the <tx:annotation-driven/>
Element in my context definition -
我的问题与我<tx:annotation-driven/>
在上下文定义中设置元素的方式有关-
Originally I had load time weaving enabled (not knownley) that read <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
and by simply removing the 2nd attribute - everything worked (took 2 hours of head banging though). I believe the 2nd element relates to the @Configurable sterotype but can let other (smarter) people explain the difference & why one would work & the other does does not.. Hope this helps...
最初我启用了加载时间编织(不知道),<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
通过简单地删除第二个属性来读取 和 - 一切正常(虽然花了 2 个小时的头部撞击)。我相信第二个元素与 @Configurable 构造型有关,但可以让其他(更聪明的)人解释差异以及为什么一个会起作用而另一个不起作用..希望这有助于...
working definition= <tx:annotation-driven transaction-manager="transactionManager"/>
工作定义= <tx:annotation-driven transaction-manager="transactionManager"/>
回答by chro
I did all the thing as a following. My problems was with "import" tag, there are several context root like servlet-context and root-context which are not dependent on each other. It becomes clear with Spring Explorer view in STS. No JTA for Tomcat.
我按照以下方式做了所有事情。我的问题是“导入”标签,有几个上下文根,如 servlet-context 和 root-context,它们不相互依赖。STS 中的 Spring Explorer 视图变得清晰。Tomcat 没有 JTA。
My advice would be universal: run Pet Clinic on your environment , How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPAor generate with Roo stub of application and try to compare your configs with referenced.
我的建议是通用的:在您的环境中运行 Pet Clinic,如何使用 Hibernate 支持的 JPA 在 tomcat 中运行 Spring 3.0 PetClinic或使用 Roo 应用程序存根生成,并尝试将您的配置与引用进行比较。
回答by Firzen
I have finally fixed this error by adding
我终于通过添加修复了这个错误
<tx:annotation-driven mode="aspectj" transaction-manager="yourTransactionManager" />
into my application-context.xml
进入我的 application-context.xml
回答by Muhammad Imran Tariq
Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin()
. This is assuming that you are using JPA outside a JTA transaction scope.
确保在执行此语句时您有一个活动的事务。如果您使用 JPA,请使用EntityManager.getTransaction().begin()
. 这是假设您在 JTA 事务范围之外使用 JPA。