I have the following criteria search开发者_如何学编程 that I would expect to return 1 project with multiple task and contexts and a single user.
What is actually getting returned is the same project multiple times for each different task.
It almost looks like I am missing a statement in the criteria to tell the search to return unique projects.
Any help would be very welcome.
ICriteria criteria = NHibernateSession.Current.CreateCriteria(typeof(Project))
.CreateAlias("User", "user")
.Add(Restrictions.Eq("user.Username", username))
.SetFetchMode("Tasks", FetchMode.Eager)
.SetFetchMode("Contexts", FetchMode.Eager);
IList<Project> projects = criteria.List<Project>();
Thanks in advance...
Not sure, but try adding criteria.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());
More info here: http://colinramsay.co.uk/diary/2008/01/15/nhibernate-optimising-queries-with-projections/
IList<Project> projects = criteria.UniqueResult<Project>();
This is also a way to solve it:
Eagerly loading entity associations efficiently with NHibernate
精彩评论