首页 > 解决方案 > 拦截器 - 如何在 onCollectionUpdate 中找到上一个快照的所有者?

问题描述

所以我重写了 Hibernate Interceptor 中的 'onCollectionUpdate' 方法,以便审计实体中的集合更改。

这是我的代码:

@Override
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
    if (collection != null) {
        PersistentCollection persistentColl = ((PersistentCollection) collection);
        Object owner = persistentColl.getOwner();
        Class<?> obj = owner.getClass();
        if (obj.isAnnotationPresent( AuditTrail.class )) {

            Map previousStoredSnapshotMap = (Map) persistentColl.getStoredSnapshot();
            Object previousCollOrMap;
            if (Collection.class.isAssignableFrom( collection.getClass() )) {
                previousCollOrMap = previousStoredSnapshotMap.values();
            } else {
                previousCollOrMap = previousStoredSnapshotMap;
            }
            String propertyName = persistentColl.getRole().substring( persistentColl.getRole().lastIndexOf( '.' ) + 1 );

            try {

                AuditTrail auditTrail = obj.getAnnotation( AuditTrail.class );
                if (auditTrail.trail()) {
                    //logging method to be added here
                }

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

        }
    }
}

我找到了“所有者”——父对象的当前状态。但是我怎样才能得到以前的所有者(previousStoredSnapshot 的所有者)?

谢谢

标签: javahibernateinterceptor

解决方案


推荐阅读