All of my NH entities derive from a type called BusinessEntity, it has the most basic values ID, Created, CreatedBy, Updated, UpdatedBy.
CreatedBy / UpdatedBy takes a User
I have a IPreUpdateEventListener and IPreInsertEventListener that fire to get the current DateTime for the audit values. Also in here I have my logic to get the current user running which I grab by running a criteria query against the windows user principle. From what I understood from all of post on the NH user group on this subject my User class needs to be eagerly loaded for this to work correctly in my EventListeners this is how I load the user
public User GetByDomainPrinciple(string domainPrinciple)
{
var domainPrincipleCriteria = DetachedCriteria.For<User>()
.Add(Restrictions.Eq("DomainPrinciple", domainPrinciple))
.SetFetchMode("Roles", FetchMode.Eager)
.SetFetchMode("Groups", FetchMode.Eager)
.SetFetchMode("Groups.Roles", FetchMode.Eager)
.SetCacheable(true);
return Repository.QuerySingle(domainPrincipleCriteria);
}
Repository.QuerySingle(domainPrincipleCriteria);
is just
return detachedCriteria
.GetExecutableCriteria(_conversation.Session).UniqueResult<T>();
Am I missing something or is my criteria query wrong? I guess absolute worst case scenario I could change CreatedBy to be a Guid instead of a User and just manually assign the FK like that, but that seems very dirty.
There was a bug in NHibernate that could be fixed with custom DefaultFlushEventListener. See answer here: https://stackoverflow.com/a/11575172/1389546
I think this might help you. The error stems from the PreUpdateEventListener. I had the same issue while back.
http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With