Hibernate, save the previous state of an object

后端 未结 5 2291
悲哀的现实
悲哀的现实 2021-01-03 09:08

Is there any generally accepted, proven-to-work way using hibernate, that keeps a history of an entitys changes in the database?

We need to keep track of quite some

5条回答
  •  余生分开走
    2021-01-03 09:39

    I've done this with an Interceptor (creating my own based on EmptyInterceptor). I was able to get all changes (merge, save, saveOrUpdate and delete) in onFlushDirty().

    The methods of EmptyInterceptor I used:

    @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 {
    

    In onFlushDirty() I had to query the previous state of the entity:

    Connection c = sessionFactory.getCurrentSession().connection();
    Session session = sessionFactory.openSession(c);
    
    BaseEntity newBaseEntity = (BaseEntity) object;
    BaseEntity oldBaseEntity = (BaseEntity) session.get(newBaseEntity.getClass(), newBaseEntity.getId());
    

提交回复
热议问题