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

后端 未结 3 1316
灰色年华
灰色年华 2021-01-19 09:04

I have 3 ways things get written to the DB

public void create(T object) {
    entityManager.persist(object);
}

public void update(T object) {
    object = e         


        
相关标签:
3条回答
  • 2021-01-19 09:20

    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

    0 讨论(0)
  • 2021-01-19 09:23

    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.

    0 讨论(0)
  • 2021-01-19 09:36

    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.

    0 讨论(0)
提交回复
热议问题