【问题标题】:How to detect collection changes in NHibernate如何检测 NHibernate 中的集合更改
【发布时间】:2011-06-29 14:47:02
【问题描述】:

如何在 NHibernate 中检测多个集合和列表的更改(添加和删除)?

我正在使用拦截器来检测对象模型中的更改(用于审计等),这对于对象内的更改(使用 OnSave/OnFlushDirty)非常有效,但不适用于集合。

当集合发生变化时,会调用 OnCollectionUpdate 方法,但它只接收持有该集合的对象的键和该集合的最新项。

我需要的是:

  1. 保存集合的对象(或至少类型+键,键仅在我的系统中的一个类中是唯一的)我现在只有键。

  2. 集合的属性名(我有多个集合的类)

  3. 添加项目列表和删除项目列表(或者,当前事务之前和之后的集合内容)。

有没有办法获得这些信息?

【问题讨论】:

    标签: nhibernate collections


    【解决方案1】:

    如果您不覆盖标准的 nhibernate 集合(列表、集合、地图),您始终可以获得集合的初始内容(在任何更改之前保存在基础中的内容)。列表示例:

    PersistentList c = myCollection as PersistentList;
    if ((c == null)||(!c.IsDirty)) {
            return;
    }
    IEnumerable storedCollection = c.StoredSnapshot as IEnumerable;
    if ((storedCollection == null)) {
            return; //looks like a error
    }
    foreach(var element in storedCollection) {
         //do something with old items
    }
    foreach(var element in storedCollection) {
         //do something with new items
    }
    

    当然,您可以弄清楚哪些元素已被添加、删除或保持不变。

    【讨论】:

    • 为什么不能将快照内容与实际的持久性集合内容进行比较?
    【解决方案2】:

    我目前正在自己​​处理这个问题。我自己还没有弄清楚你的第三个问题(我正在努力找出答案),但这应该可以帮助你解决 #1 和 #2:

    public static void SomeMethod(this ISession session, object entity)
    {            
        //this will give you the information about your entity
        String className = NHibernateProxyHelper.GuessClass(entity).FullName;
        ISessionImplementor sessionImpl = session.GetSessionImplementation();
        IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);
    
        //at this point you can use the persister object to retrieve the info you need:
    
        //this gives you all properties and their values
        Object[] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);
    
        //this will return the types:
        IType[] typeArray = persister.PropertyTypes;
    
        //each IType object has a IsCollectionType bool which tells you if it is a collection:
        bool isCollection = IType[0].IsCollectionType;
    }
    

    【讨论】:

      【解决方案3】:

      也许有点棘手,但是创建一个自定义代理呢?

      【讨论】:

      • 我真的不明白这对我有什么帮助。我在检测普通对象属性的变化时没有问题,只是在集合中 - 并且不会在代理上调用集合的 Add 和 Remove 方法(此外,这不适用于未延迟加载的对象)
      • 如果我们使用博客数据模型,例如,当有人调用 post.Comments.Add 时,会在代理上调用 Comments 属性 getter 并且可以被它捕获 - 但这并不能告诉我Add 被调用(据我所知,它可能是来自 foreach 循环的 GetEnumerator),但它没有告诉我添加了什么。
      【解决方案4】:

      这不是 NHibernate 代码(它是 Hibernate Java 代码),但是通过谷歌搜索 NHibernate API 看起来类似的代码也可以让您开始使用 NHibernate(有一个 PersistentMap 类具有Owner 属性和CollectionSnapshot 属性):

      public void onCollectionUpdate(Object collection, Serializable id) {
          System.out.println("****onCollectionUpdate");
      
          if(collection instanceof PersistentMap) {
              PersistentMap newValues = (PersistentMap) collection;
              Object owner = newValues != null ? newValues.getOwner() : null;
              Set<?> oldValues = newValues != null
                  ? ((Map<?, ?>) newValues.getStoredSnapshot()).keySet()
                  : null;
      
              System.out.println("owner: " + (owner != null ? owner.toString() : "(null)"));
              System.out.println("oldValues: " + (oldValues != null ? oldValues.toString() : "(null)"));
              System.out.println("newValues: " + (newValues != null ? newValues.toString() : "(null)"));
          } else if (collection instanceof PersistentSet) {
              PersistentSet newValues = (PersistentSet) collection;
              Object owner = newValues != null ? newValues.getOwner() : null;
              Set<?> oldValues = newValues != null
                  ? ((Map<?, ?>) newValues.getStoredSnapshot()).keySet()
                  : null;
      
              System.out.println("owner: " + (owner != null ? owner.toString() : "(null)"));
              System.out.println("oldValues: " + (oldValues != null ? oldValues.toString() : "(null)"));
              System.out.println("newValues: " + (newValues != null ? newValues.toString() : "(null)"));
          }
      }
      

      请注意,我知道这不是 C# 或 NHibernate 代码。如果这对 NHibernate 完全没用,请发表评论(如果 API 根本不相似,即使我的谷歌搜索说它是相似的),我将删除该帖子。

      我现在正沉浸在 Java 领域,或者我会为你尝试一下:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-09
        • 2018-04-17
        • 2021-07-03
        • 1970-01-01
        • 1970-01-01
        • 2020-11-18
        相关资源
        最近更新 更多