Hibernate Envers - Doesn't write audit records for createQuery(…).executeUpdate(), only .persist() and .merge()

和自甴很熟 提交于 2019-12-01 19:58:33

It looks like Avinash T. is right - if you want to create native SQL query, use createNativeQuery(String sqlString) method of EntityManager. Using createQuery(String ejbqlString) is only possible if you're using EJB QL. Hope it would help.

No, Envers won't work if you are using executeUpdate. That is because the update doesn't pass through Hibernate's event mechanism, so Envers has no chances of intercepting the change, and writing the audit.

Try this -

    public int updateStatus(String id, String status) {

    final int changes =
                entityManager.createQuery("Update Item set state = :newState," +
                        " lastModified = CURRENT_TIMESTAMP" +
                        " where id = : id ")
                    .setParameter("newState", status)
                    .setParameter("id", id)
                    .executeUpdate();

            return changes;
}

Following link wiil help you to learn more about JPQL -

http://docs.oracle.com/javaee/6/tutorial/doc/bnbtg.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!