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);
}
}
You can try code like this to reset the entity so that NH thinks it wasn't changed: