views:

239

answers:

1

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.

+1  A: 

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

epitka
Unless I'm missing something useful in the comments this isn't helpful this is where I got the code for my event listener except I call NH from the event listener to get the user instead of just using the windows principle like his example.
Chris Marisic
epitka