下面是方法:
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)
可能是因为删除查询启动
DELETE FROM entity_name [[AS] identification_variable] [WHERE <filter>]
您的查询本可以写成
"DELETE from Vote o where o.memberReply.id = 'someId'"
此外,DELETE查询只能在活动事务中执行。你会想要
em.getTransaction().begin();
query.executeUpdate();
em.getTransaction().commit();
以及
catch (Exception e) {em.getTransaction().rollback();}