views:

35

answers:

3

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...

+2  A: 

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/

Pedro
+1  A: 

IList<Project> projects = criteria.UniqueResult<Project>();

limpalex
this will actually return only one result, what he wants is Distinct
Pedro
UniqueResult doesnt return a list... https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Criteria.html#uniqueResult()
Pedro
Pedro I should have mentioned distinct in the post
Burt
+1  A: 

This is also a way to solve it. Eagerly loading entity associations efficiently with NHibernate http://bit.ly/4RZGz3

cws