问题描述
我有3种方法将数据写入数据库
public void create(T object){
entityManager .persist(对象);
}
public void update(T object){
object = entityManager.merge(object);
$ b public int updateStatus(String id,String status){
final int changes =
entityManager.createQuery(update item set state = :newState+
last_modified = current_timestamp+
where id =:id)
.setParameter(newState,status)
.setParameter(id ,id)
.executeUpdate();
返回更改;
$ / code>
我得到的问题是为了让Hibernate Envers实际写入将审计记录记录到corrsponsing x_aud和revinfo DB表中。它仅适用于'.persist()'或'.merge()'。我无法让它适用于'createQuery(...) .executeUpdate()'
我错过了什么,为此工作。问题是,我的很多代码都是使用.executeUpdate编写的,而不是合并,所以我真的需要使用现有的代码。
任何人都可以帮忙它看起来像 Avinash T。是对的 - 如果你想创建原生SQL查询,使用 createNativeQuery(String sqlString)
EntityManager
的方法。使用 createQuery(String ejbqlString)
仅当您使用EJB QL时才可能。希望这会有所帮助。
I have 3 ways things get written to the DB
public void create(T object) {
entityManager.persist(object);
}
public void update(T object) {
object = entityManager.merge(object);
}
public int updateStatus(String id, String status) {
final int changes =
entityManager.createQuery("update item set state = :newState," +
" last_modified = current_timestamp" +
" where id = : id ")
.setParameter("newState", status)
.setParameter("id", id)
.executeUpdate();
return changes;
}
The problem I have, is in order to get the Hibernate Envers to actually write the audit records to the corrsponsing x_aud and revinfo DB tables. It only works successfully for '.persist()' or '.merge()'. I cannot get it to work for 'createQuery(...).executeUpdate()'
Am I missing something or does it just not work for this. The problem is, a lot of my code has been written using .executeUpdate and not merge, so really I need this to work with the existing code.
Can anyone help please?
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.
这篇关于Hibernate Envers - 不为createQuery(...)写出审计记录。executeUpdate(),只有.persist()和.merge()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!