javax.persistence.TransactionRequiredException:

javax.persistence.TransactionRequiredException:,java,exception,jpa,persistence,Java,Exception,Jpa,Persistence,我试图使用JPA控制器方法从表中删除记录,但出现以下异常 “javax.persistence.TransactionRequiredException:如果实体管理器尚未加入当前事务,则无法调用需要事务的方法。” 下面是我试图运行的代码 public void deleteRulesofType(String ruleType) throws NotSupportedException, SystemException, Exception { EntityManager em =

我试图使用JPA控制器方法从表中删除记录,但出现以下异常

“javax.persistence.TransactionRequiredException:如果实体管理器尚未加入当前事务,则无法调用需要事务的方法。”

下面是我试图运行的代码

 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();
    }

}

非常感谢您的任何帮助:)

请尝试通过拨打电话让您的实体经理加入交易

em.joinTransaction();

感谢@elefasGR,我删除了em.flush()行并添加了em.jointransation(),然后清理并构建了项目。但是当我运行它时,我仍然会遇到异常:(你能用你提到的更改更新你的代码吗?对不起..我的错。我忘记在query.executeUpdate()之前调用em.joinTransaction();。这就是为什么。现在它工作得很好..非常感谢:)