views:

28

answers:

1

I have a type Type1 with a property Type2s consisting of a List<Type2>. I have configured the NHibernate mapping for the property as follows:

[Map(0, Table = "Type2s", Schema = "MySchema", Cascade = CascadeStyle.All, Lazy = true, Inverse = true)]
[Key(1, Column = "Type1Id")]
[OneToMany(2, Class = "Type2, MyNamespace")]

What is the difference in behavior between these two criteria for retrieval of Type1 instances:

var criteria1 = DetachedCriteria.For<MyType1>();

var criteria2 = DetachedCriteria.For<MyType1>().SetFetchMode("Type2s", FetchMode.Join);

When I invoke .List on the executable criteria from these criteria, presumably the SQL to retrieve the Type2s associated with each Type1 instance is not run until I actually attempt to access the property because the property is marked as being Lazy. Is this a correct assumption?

If I want to force evaluation of the Lazy property how can this best be achieved?

+1  A: 

I'm not very familiar with attribute binding vs. hbm/fluent, but I believe you have solved your own problem!

You are correct assuming that the data for the Type2s will not be loaded until request because of the bag being lazy. Your second criteria's fetchmode forces those object to be hydrated at the same time, ie. in one database roundtrip.

As an aside, FetchMode.Join is equivalent to FetchMode.Eager (which is a better name re:laziness in my opinion).

article explaining it a little more in depth: http://davidhayden.com/blog/dave/archive/2008/12/06/ImprovingNHibernatePerformanceFetchingStrategiesFetchModeFluentNHibernate.aspx

describing FetchMode.Join and FetchMode.Eager: http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx

ddango
Excellent explanation, thanks.
Ben Aston