NHibernate PocoEntityTuplizer is setting entities to "dirty". How do I stop this?

355 views Asked by At

Some of my objects in my database use 0 for a non existent relationship. So I setup a NullableTuplizer class. I found the code for this online. It has worked so far, but I noticed an issue. We have been having major problems with entities being dirty immediately after being selected. So immediately after a Get() over this object, its dirty, and NHibernate attempts to save it to the database.

I am trying to prevent these "mapping problems". Does anyone know how to make it know that it is really NOT dirty?

public class NullableTuplizer : PocoEntityTuplizer
{
    public NullableTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity)
        : base(entityMetamodel, mappedEntity)
    {
    }

    public override object[] GetPropertyValuesToInsert(
        object entity, IDictionary mergeMap, ISessionImplementor session)
    {
        object[] values = base.GetPropertyValuesToInsert(entity, mergeMap, session);
        //dirty hack 1
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i] == null && typeof(BaseEntity).IsAssignableFrom(getters[i].ReturnType))
            {
                values[i] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }

    public override object[] GetPropertyValues(object entity)
    {
        object[] values = base.GetPropertyValues(entity);
        //dirty hack 2
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i] == null && typeof(BaseEntity).IsAssignableFrom(getters[i].ReturnType))
            {
                values[i] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }


    public override void SetPropertyValues(object entity, object[] values)
    {
        //dirty hack 3.
        for (int i = 0; i < values.Length; i++)
        {
            dynamic val = values[i];
            if (typeof(BaseEntity).IsAssignableFrom(getters[i].ReturnType) && val.Id == 0)
            {
                values[i] = null;
            }
        }
        base.SetPropertyValues(entity, values);
    }
}
1

There are 1 answers

0
Ricardo Peres On

You can try code like this to reset the entity so that NH thinks it wasn't changed:

public static void Reset(this ISession session, Object entity)
    {
        var sessionImpl = session.GetSessionImplementation();
        var context = sessionImpl.PersistenceContext;
        var entry = context.GetEntry(context.Unproxy(entity));

        if ((entry == null) || (entry.RequiresDirtyCheck(entity) == false) || (entry.ExistsInDatabase == false) || (entry.LoadedState == null))
        {
            return;
        }

        var persister = entry.Persister;
        var currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);
        var loadedState = entry.LoadedState;

        for (var 0; i < currentState.Length; ++i)
        {
            loadedState[i] = currentState[i];
        }
    }