TransactionRequiredException Executing an update/delete query

后端 未结 20 2073
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:29

Hi I am using hibernate JPA with spring and mongodb and i am running my application on Glassfish-4.0.

My service class is :

@Component
public class T         


        
相关标签:
20条回答
  • 2020-12-02 15:54

    Error message while running the code:

    javax.persistence.TransactionRequiredException: Executing an update/delete query

    Begin the entityManager transaction -> createNativeQuery -> execute update -> entityManager transaction commit to save it in your database. It is working fine for me with Hibernate and postgresql.

    Code

    entityManager.getTransaction().begin();
    Query query = entityManager.createNativeQuery("UPDATE tunable_property SET tunable_property_value = :tunable_property_value WHERE tunable_property_name = :tunable_property_name");
    query.setParameter("tunable_property_name", tunablePropertyEnum.eEnableJobManager.getName());
    query.setParameter("tunable_property_value", tunable_property_value);
    query.executeUpdate();
    entityManager.getTransaction().commit();
    
    0 讨论(0)
  • 2020-12-02 15:57

    I faced the same exception "TransactionRequiredException Executing an update/delete query" but for me the reason was that I've created another bean in the spring applicationContext.xml file with the name "transactionManager" refering to "org.springframework.jms.connection.JmsTransactionManager" however there was another bean with the same name "transactionManager" refering to "org.springframework.orm.jpa.JpaTransactionManager". So the JPA bean is overriten by the JMS bean.

    After renaming the bean name of the Jms, issue is resolved.

    0 讨论(0)
  • 2020-12-02 15:58

    I Got the same error.

    I just added the @Transactional annotation of javax.transaction.Transactional on the method.

    0 讨论(0)
  • 2020-12-02 15:59

    Just add @Transactional on method level or class level. When you are updating or deleting record/s you have to maintain persistence state of Transaction and @Transactional manages this.

    and import org.springframework.transaction.annotation.Transactional;

    0 讨论(0)
  • 2020-12-02 16:02

    How about moving @Transactional from method to class level? Worked for me in similar case, though I'm not 100% certain why.

    0 讨论(0)
  • 2020-12-02 16:03

    Nothing seemed to work for me until I realized that my method was declared as public final instead of just public. The error was being caused by the final keyword. Removing it made the error go away.

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