Hi,
I found a (messy) way to get this working:
public class NullEventListener : IPreUpdateEventListener, IPreInsertEventListener, IPreLoadEventListener
{
#region IPreInsertEventListener Members
public bool OnPreInsert(PreInsertEvent preInsertEvent)
{
var instance = preInsertEvent.Entity as MyTable;
if (instance == null)
return false;
if (instance.Parent == null)
Set(preInsertEvent.Persister, preInsertEvent.State, "Parent", string.Empty);
return false;
}
#endregion
#region IPreLoadEventListener Members
public void OnPreLoad(PreLoadEvent preLoadEvent)
{
var instance = preLoadEvent.Entity as MyTable;
if (instance == null)
return;
try
{
// this is really messy!!
var parent = Get(preLoadEvent.Persister, preLoadEvent.State, "Parent") as MyTable;
if (parent == null || parent.ID == "")
throw new Exception("Set to null");
}
catch (Exception)
{
Set(preLoadEvent.Persister, preLoadEvent.State, "Parent", null);
}
return;
}
#endregion
#region IPreUpdateEventListener Members
public bool OnPreUpdate(PreUpdateEvent preUpdateEvent)
{
var instance = preUpdateEvent.Entity as MyTable;
if (instance == null)
return false;
if (instance.Parent == null)
Set(preUpdateEvent.Persister, preUpdateEvent.State, "Parent", string.Empty);
return false;
}
#endregion
private static void Set(IEntityPersister persister, object[] state, string propertyName, object value)
{
int index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1)
return;
state[index] = value;
}
private static object Get(IEntityPersister persister, object[] state, string propertyName)
{
int index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1)
return null;
return state[index];
}
}
Thanks and regards,
forki