【问题标题】:javax.persistence.TransactionRequiredException:javax.persistence.TransactionRequiredException:
【发布时间】:2016-03-07 10:52:57
【问题描述】:

我正在尝试使用 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();
    }

}

非常感谢任何帮助:)

【问题讨论】:

    标签: java exception jpa persistence


    【解决方案1】:

    尝试通过调用让你的实体经理加入事务

    em.joinTransaction();
    

    【讨论】:

    • 感谢@elefasGR,我删除了 em.flush() 行并添加了 em.joinTransaction() 然后清理并构建项目。但是当我运行它时我仍然得到异常:(
    • 您能用您提到的更改更新您的代码吗?
    • 对不起..我的错。我忘了在 query.executeUpdate(); 之前调用 em.joinTransaction()。这就是为什么。现在它完美运行.. 非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2022-06-28
    相关资源
    最近更新 更多