tags:

views:

75

answers:

2

Hi,

I want to get an entity with its sub-properties. How can I do that with nhibernate without indicating in mapping. I want to control the fetching like we can do in linq-to-sql by "LoadWith()" method. Not an automated solution.

public class Survey
{
   public virtual long Id;
   public virtual String Title;
   public virtual IList<SurveyPage> Pages; 
}

I want to get a survey with its Pages properties loaded, in exact one query without using mapping file. How can I do that in nhibernate?

+1  A: 

Solved. This did what I want.

IList<Survey> surveys = session.CreateCriteria(typeof(Survey))
                        .SetFetchMode("SurveyPages", FetchMode.Eager)
                        .List<Survey>();
yapiskan
FetchMode.Eager is obsolete, use FetchMode.Join instead. It is actually the same value, but another name.
Stefan Steinegger
A: 

Yes, the FetchMode is one neat of doing it. Couple of things to notice are

  1. You can only set the fetchmode to one collection (at a time) of a mapping file.

  2. When you do a fetch mode on associated Collections, it would return a join of rows.(Will not separate the unique rows of parent).

Another way of doing the same is to use an HQl query with Join.

Sree