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
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();
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.
I Got the same error.
I just added the @Transactional annotation of javax.transaction.Transactional on the method.
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;
How about moving @Transactional
from method to class level?
Worked for me in similar case, though I'm not 100% certain why.
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.