在NHibernate中,我检查实体是否脏失败 [英] In NHibernate, my check if entity is dirty fails

查看:93
本文介绍了在NHibernate中,我检查实体是否脏失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于这个问题,我需要确定NHibernate应用程序中的实体是否脏. ISession上有一个"IsDirty"方法,但是我想检查一个特定的实体,而不是整个会话.

Similar to this question, I need to determine if an entity in my NHibernate application is dirty or not. There is a "IsDirty" method on ISession, but I want to check a specific entity, not the whole session.

有关nhibernate.info的帖子描述了通过获取实体的数据库状态并将其与实体的当前状态进行比较来检查实体的方法.

This post on nhibernate.info describes a method of checking an entity by fetching its database state and comparing it to the current state of the entity.

我已经复制了该方法,但是它对我不起作用.参见代码:

I've copied that method, but it isn't working for me. See the code:

public static Boolean IsDirtyEntity(this ISession session, Object entity)
{
    String className = NHibernateProxyHelper.GuessClass(entity).FullName;
    ISessionImplementor sessionImpl = session.GetSessionImplementation();
    IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;
    IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);
    EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);


    if ((oldEntry == null) && (entity is INHibernateProxy))
    {
        INHibernateProxy proxy = entity as INHibernateProxy;
        Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);
        oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);
    }

    Object [] oldState = oldEntry.LoadedState;
    Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);
    Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);

    return (dirtyProps != null);
}

通过调用persister.GetPropertyValues()填充currentState数组的行是问题所在.该数组充满了空值,而不是我实体的实际值.

The line that that populates the currentState array by calling persister.GetPropertyValues() is the problem. The array is full of nulls, instead of the actual values from my entity.

当我进入代码时,我发现反射用于从字段中获取值-但字段为空.这似乎是代理的结果,但我不确定从何处去.

When I stepped into the code, I found that reflection is being used to get the values from the fields -- but the fields are null. This seems like a result of the proxy, but I'm not quite sure where to go from here.

推荐答案

我将默认访问策略从"field.camelcase-underscore"更改为"property",现在persister.GetPropertyValues()方法返回正确的值.

I changed my default-access strategy from "field.camelcase-underscore" to "property" and now the persister.GetPropertyValues() method returns correct values.

现在宣布胜利还为时过早,但似乎很有趣.我之所以使用字段访问策略,是因为我在实体的属性中有代码来跟踪脏状态.由于我要删除该代码并依靠NH来确定脏状态,因此我可以使用属性访问策略.

Too early to declare victory, but seems interesting. I was using the field access strategy because I had code in my entities' properties to track dirty state. Since I'm removing that code and going to rely on NH to determine dirty state, I was able to use the property access strategy.

这篇关于在NHibernate中,我检查实体是否脏失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆