Hibernate + Spring:javax.persistence.TransactionRequiredException:没有事务正在进行 [英] Hibernate + Spring: javax.persistence.TransactionRequiredException: no transaction is in progress

查看:2096
本文介绍了Hibernate + Spring:javax.persistence.TransactionRequiredException:没有事务正在进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Spring和Hibernate设置一个项目。在遇到其他几个问题后,我得到了下一个问题:无论如何,P



。当调用 entityManager.flush()时,我得到一个异常: javax.persistence.TransactionRequiredException:没有事务正在进行

我用 @Transactional 注释了我的类,但它仍然不起作用。

我在这里阅读了人们有类似问题的线程线程,但解决方案总是< tx:annotation-driven transaction-manager = transactionManager/> 。但我已经有了这个配置。

我不确定我应该提供哪些代码或文件,因此您可以在这里找到整个项目:
https://drive.google.com/file/d/0B7Exc4dHICl-UDN3ZEJnbS1FZFk/edit?usp=sharing



预先感谢您。

解决方案

我已经通过实现 ApplicationListener< ContextRefreshedEvent> 并将自我引用放到服务调用中。

问题出现是因为在调用方法中没有开始事务,并且因为后续的方法调用是内部引用,所以没有新的交易已创建。因此,我必须将我的调用方法设置为事务性的,或者创建一个内部引用,并通过该引用调用后续方法。



必须将以下方法覆盖为

  private MyService myService = myService; 
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
myService = event.getApplicationContext()。getBean(MyService.class);

然后使用 myService.myMethod()而不是 myMethod()您希望事务处于活动状态。


I'm currently setting up a project with Spring and Hibernate. After several other problems i got the next problem :P

Anyway. When call entityManager.flush() I get an exception: javax.persistence.TransactionRequiredException: no transaction is in progress.

I have annotated my class with @Transactionalbut it's still not working.

I read serval threads here where people had a similar problem, but the solution was always <tx:annotation-driven transaction-manager="transactionManager" />. But I already have this configuration.

I am not sure what code or files I should provide you, so you can find the whole project here: https://drive.google.com/file/d/0B7Exc4dHICl-UDN3ZEJnbS1FZFk/edit?usp=sharing

Thank you in advance.

解决方案

I have found solution of this issue by implementing ApplicationListener<ContextRefreshedEvent> and putting a self reference to the service call.

The issue arise because there is no transaction started in the calling method and since subsequent method calls are internal reference so no new transaction was created. So, I have to either make my calling method as transactional or create an internal reference and call the subsequent method through that reference.

You have to override the following method as

private MyService myService = myService;    
@Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            myService = event.getApplicationContext().getBean(MyService.class);
        }

Then use myService.myMethod() instead of myMethod() where ever you want transaction to be active.

这篇关于Hibernate + Spring:javax.persistence.TransactionRequiredException:没有事务正在进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆