如何获得previouseState,而不使用Hibernate拦截器从数据库中获取创建审计跟踪表? [英] how to get previouseState without fetching from the db using hibernate interceptor to create audit trail table?
问题描述
entity,id,property,oldValue,newValue
我试图创建一个审计表, ,用户,时间戳
使用hibernate EmptyInterceptor
我希望这个表有很多的IO。看来previouseState在onFlushDirty方法没有按我期望的那样工作。
如何获取旧值而无需从db中获取它,如这里?
我试图遵循这篇文章
public class AuditLogInterceptor extends EmptyInterceptor {
private Set inserts = new HashSet();
私人设置更新=新的HashSet();
private设置deletes = new HashSet();
私人地图oldies = new HashMap();
@Override
public boolean onSave(Object entity,
Serializable id,
Object [] state,
String [] propertyNames,
类型[]类型)
抛出CallbackException {
if(entity instanceof Auditable)
inserts.add((Auditable)entity);
返回false;
$ b @Override
public void onDelete(Object entity,Serializable id,Object [] state,
String [] propertyNames,Type [] types){
if(实体instanceof可审计的)
deletes.add((可审计的)实体);
$ b @Override
public boolean onFlushDirty(Object entity,
Serializable id,
Object [] currentState,
Object [] previousState ,
String [] propertyNames,
类型[]类型)
抛出CallbackException {
if(entity instanceof Auditable){
try {
// //使用从数据库中获取更新前状态的id和class
Session tempSession =
HibernateUtil.getSessionFactory()。openSession();
可审计的旧=(可审计)tempSession.get(entity.getClass(),((可审计)实体).getId());
oldies.put(old.getId(),old);
updates.add((Auditable)entity);
} catch(Throwable e){
_log.error(e,e);
}
}
返回false;
}
有没有办法让get pre-update状态而不是往返数据库? 解决方案
另一种方法是将以前的状态加载。 / p>
@Entity
@Table(name ='Foo')
class Foo {
@Transient
private Foo previousState;
@PostLoad
private void setPreviousState(){
previousState = new Foo();
//复制字段
}
public Foo getPreviousState(){
return previousState;
}
}
I am trying to create an Audit table that will ends looking something like that :
entity,id,property,oldValue,newValue,user,timestamp
using hibernate EmptyInterceptor I expect that this table will have a lot of IO's . It seems that previouseState at onFlushDirty method is not working as I expected .
how can I get the old value without fetching it from the db as shown here ?
I was trying to follow this post
public class AuditLogInterceptor extends EmptyInterceptor {
private Set inserts = new HashSet();
private Set updates = new HashSet();
private Set deletes = new HashSet();
private Map oldies = new HashMap();
@Override
public boolean onSave(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types)
throws CallbackException {
if (entity instanceof Auditable)
inserts.add((Auditable)entity);
return false;
}
@Override
public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
if (entity instanceof Auditable)
deletes.add((Auditable)entity);
}
@Override
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types)
throws CallbackException {
if (entity instanceof Auditable) {
try {
// Use the id and class to get the pre-update state from the database
Session tempSession =
HibernateUtil.getSessionFactory().openSession();
Auditable old = (Auditable) tempSession.get(entity.getClass(), ((Auditable) entity).getId());
oldies.put(old.getId(), old);
updates.add((Auditable)entity);
} catch (Throwable e) {
_log.error(e,e);
}
}
return false;
}
is there a way of getting the get the pre-update state without a round trip to the db ?
An alternative approach is to store the previous state on load.
@Entity
@Table(name='Foo')
class Foo {
@Transient
private Foo previousState;
@PostLoad
private void setPreviousState(){
previousState = new Foo();
//copy the fields
}
public Foo getPreviousState(){
return previousState;
}
}
这篇关于如何获得previouseState,而不使用Hibernate拦截器从数据库中获取创建审计跟踪表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!