Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress

后端 未结 13 2217
说谎
说谎 2020-12-05 17:52

When I call:

entityManager.flush()

I get the exception mentioned in the title.

I am using Hibernate JPA.

相关标签:
13条回答
  • 2020-12-05 18:31

    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.

    Once the @Service annotation was removed from the service class in question, and an appropriate bean was declared in the XML config:

    <bean id="myService" class="com.example.myapp.service.MyServiceImpl" />
    

    the problem is gone.

    Check this JIRA bug for more details.

    0 讨论(0)
  • 2020-12-05 18:32

    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.

    My advice would be universal: run Pet Clinic on your environment , How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA or generate with Roo stub of application and try to compare your configs with referenced.

    0 讨论(0)
  • 2020-12-05 18:33

    Make sure that your spring configuration includes the following line:

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

    mode can be either proxy or aspectj and transaction-manager has to point to your transaction manager been.

    0 讨论(0)
  • 2020-12-05 18:37

    Please make sure that your handler method is declared as public

    @Transactional 
    @RequestMapping('/test')
    public String doTest() {
        // do your stuff here 
        return 'testview';
    }
    
    0 讨论(0)
  • 2020-12-05 18:40

    I had this problem, just add @Transacctional annotation not only on the method, also in the class together with your @Service annotation.

    for example:

    @Service
    @Transactional
    public class MyService {
    
    }
    
    0 讨论(0)
  • 2020-12-05 18:41

    I have finally fixed this error by adding

    <tx:annotation-driven mode="aspectj" transaction-manager="yourTransactionManager" />
    

    into my application-context.xml

    0 讨论(0)
提交回复
热议问题