在EntityManager中创建删除查询时出现异常

时间:2012-07-13 11:51:33

标签: sql jpa jpql entitymanager

以下是方法:

public void deleteVotesByReplyID(long replyId) {
        EntityManager em = getEntityManager();
        try {
           int re = em.createQuery("delete object(o) 
                                    from Vote as o 
                                    where o.memberReply.id = '"+replyId+"'"
                                  ).executeUpdate();       
        } finally {
            em.close();
        }
    }

上述查询有什么问题? (用过jpa 1.0)

2 个答案:

答案 0 :(得分:1)

也许是因为删除查询开始

DELETE FROM entity_name [[AS] identification_variable] [WHERE <filter>]

答案 1 :(得分:0)

您的查询可能已写为

"DELETE from Vote o where o.memberReply.id = 'someId'"

DELETE查询也只能在活动事务中执行。你会想要

em.getTransaction().begin();
query.executeUpdate();
em.getTransaction().commit();

还有

catch (Exception e) {em.getTransaction().rollback();}