2

Consider the following scenario:

Class A has a one-to-many relationship to Class B. Class B has a many-to-one relationship to Class C.

class A {
  IList<B> BList {get;set;}
}
class B {
  C CMember{get;set;}
}

class C {
   //null
}

If I load class B from the database using something like

   IList<B> result = query.List<B>();

everything works as expected,

However if I do something like:

   DetachedCriteria query = DetachedCriteria.For(typeof(A));
   query.CreateAlias("B", "B", JoinType.InnerJoin);
   IList<A> result = query.List<A>();

then, NHibernate will also select from table C and load all Cs.

Mappings below: A mapping...

<bag name="BList " table="B" lazy="true" inverse="false">
    <key column="id" />
    <one-to-many class="B" />
    </bag>

B mapping...

<many-to-one class="C" name="CMember" column="idC" lazy="proxy" outer-join="true" />

Any Ideas? Thanks.

1
  • You got your hibernate session still open when you're trying to access the lazy-loaded collections? Commented Jul 20, 2010 at 0:53

1 Answer 1

1

I have created a sample app to test the scenario you outline and I was not able to reproduce a difference between the Criteria and the DetachedCriteria, they both returned the same result for me.

Whether or not the load obeys the lazy property was dependant on the outer-join attribute, when it is set I always got C loaded eagerly, when I removed the outer-join attribute it lazy loaded C through a proxy.

So one possible solution is to change the B mapping to:

<many-to-one class="C" name="CMember" column="idC" lazy="proxy"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.