Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate exception collection [..User.Groups] was not processed by flush()

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.

like image 620
Chris Marisic Avatar asked Jan 06 '10 20:01

Chris Marisic


2 Answers

There was a bug in NHibernate that could be fixed with custom DefaultFlushEventListener. See answer here: https://stackoverflow.com/a/11575172/1389546

like image 50
desunit Avatar answered Oct 21 '22 05:10

desunit


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

like image 42
epitka Avatar answered Oct 21 '22 06:10

epitka