1

I am trying to delete records from a table using JPA Controller method, but I get the following exception..

"javax.persistence.TransactionRequiredException: Cannot call methods requiring a transaction if the entity manager has not been joined to the current transaction."

Following is the code I am trying to run

 public void deleteRulesofType(String ruleType) throws NotSupportedException, SystemException, Exception { EntityManager em = getEntityManager(); try { utx.begin(); Query query = em.createQuery("delete from RulesFound r where r.ruleType=:ruleType"); query.setParameter("ruleType", ruleType); query.executeUpdate(); em.flush(); utx.commit(); } catch (Exception e) { try { utx.rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } throw e; } finally { em.close(); } } 

Any help is greatly appreciated :)

1 Answer 1

2

Try to make your entity manager to join the transaction by calling

em.joinTransaction(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @elefasGR, I removed the em.flush() line and added em.joinTransaction() and then clean and build the project. But I still get the exception when I run it :(
Can you update your code with the changes you mention please?
sorry.. my bad. I forgot to call em.joinTransaction() before query.executeUpdate();. That's why. Now it works perfect.. Thank you so much :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.