Hibernation, saving the previous state of the object

Is there a generally accepted, proven way to use hibernate that stores a history of entity changes in a database?

We need to track some objects, and we want to be able to undo changes to objects.

I tried using sleep mode interceptors, but the old state of the object is only available at runtime merge(). We mainly do update()or saveOrUpdate(), though ...

I am currently authorizing warp-persist with Guice to manage the session.

+3
source share
5 answers

Interceptor ( EmptyInterceptor). (merge, save, saveOrUpdate ) onFlushDirty().

EmptyInterceptor :

@Override
public boolean onFlushDirty(Object object, Serializable id,
    Object[] newValues, Object[] oldValues, String[] properties,
    Type[] types) throws CallbackException {

@Override
public boolean onSave(Object object, Serializable id, Object[] newValues,
    String[] properties, Type[] types) throws CallbackException {

@Override
public void onDelete(Object object, Serializable id, Object[] newValues,
    String[] properties, Type[] types) throws CallbackException {

onFlushDirty() :

Connection c = sessionFactory.getCurrentSession().connection();
Session session = sessionFactory.openSession(c);

BaseEntity newBaseEntity = (BaseEntity) object;
BaseEntity oldBaseEntity = (BaseEntity) session.get(newBaseEntity.getClass(), newBaseEntity.getId());
+4

JBoss envers - , .

Envers , / . , , , , @Audited. , , . .

Subversion, . , ( ). , , , () . , , , , .
+1

Hibernate Envers JBoss - , , , :

Envers / . , , , , @Audited. , , . .

+1

, JBoss: envers. , , , .

+1

I had exactly the same problem. One way: lock()with the previous state, and then merge()with the new state, instead of update()only doing with the new state.

Then hibernate knows about before / after, and it is available in interceptor / event listeners.

NTN.

0
source

Source: https://habr.com/ru/post/1726039/


All Articles