andy
andy

Reputation: 653

javax.persistence.TransactionRequiredException:

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 :)

Upvotes: 1

Views: 2611

Answers (1)

pleft
pleft

Reputation: 7905

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

em.joinTransaction();

Upvotes: 2

Related Questions