Search code examples
nhibernateconform

How does fetch work


Possible Duplicate:
NHibernate Join Fetch(Kind)

I have read article. http://fabiomaulo.blogspot.com/2010/03/conform-mapping-components.html

Tried to reproduce it on my project and got strange behavioral.

I have two entites:

Plan 
{ 
    public virtual int Id { get; set; } 
    public virtual Payer Payer { get; set; } 
} 

Payer 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 

Mapping for it:

 orm.Cascade<Payer, Plan >(CascadeOn.Persist | CascadeOn.Merge); 
 orm.Cascade<Plan, Payer>(CascadeOn.None); 

Fetch:

mapper.Customize<CarePlan>(cm => cm.ManyToOne(o => o.Payer, x => 
x.Fetch(FetchKind.Join))); 

Simple get:

    var plan = session.Query<Plan>().Where(c=>c.Id == 
    1).SingleOrDefault(); 

 Console.WriteLine(plan.Payer.Name); 

And I got this queries:

select careplan0_.Id 
       careplan0_.PayerId 
from   CarePlans careplan0_ 
where  careplan0_.Id = 1 /* @p0 */ 
and other query 
SELECT payer0_.Id , 
payer0_.Name    as Name 
FROM   Payers payer0_ 
WHERE  payer0_.Id = 2 

Why I got 2 queries? How can I get one query with join?


Solution

  • Try

    var plan = session.Query() .Where(c=>c.Id == 1) .Fetch(x => x.Player).Eager .SingleOrDefault();

    Try disabling lazy load, so it will eager load by default