Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Executing an update/delete query" exception for @NamedQuery doing REMOVE

The following exception thrown for Spring Batch application:

19:12:40,083 ERROR main AbstractStep:213 - Encountered an error executing the step
javax.persistence.TransactionRequiredException: Executing an update/delete query

Code,where named query used:

entityManagerFactory.createEntityManager()
                    .createNamedQuery("removeQuery").executeUpdate();

also tried to wrap this code in begin and commit methods of EntityTransaction object and, didn't help:

EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
entityManagerFactory.createEntityManager()
                    .createNamedQuery("removeQuery").executeUpdate();
transaction.commit();
em.close();
entityManagerFactory.close();

thank you in advance

like image 376
sergionni Avatar asked Jun 03 '10 16:06

sergionni


1 Answers

You don't use same entity manager to create your transaction and to create your query.

Replace

entityManagerFactory.createEntityManager()
                    .createNamedQuery("removeQuery").executeUpdate();

by

em.createNamedQuery("removeQuery").executeUpdate();
like image 189
sacrepit Avatar answered Nov 10 '22 17:11

sacrepit