I think you need to specify the fetch mode when building your
criteria:
.SetFetchMode("ZoneWidgets", FetchMode.Eager)

http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-aggregate-with-many-child-collections.aspx



On 22 sep, 18:24, Scott <[email protected]> wrote:
> I'm wondering if someone could help me resolve an issue where I'm
> trying to eager fetch a collection  (one to many) assocation using
> ICriteria.  The upfront SQL looks valid as it is retrieving the
> collection properties data but what happens is that when I then access
> my collection property, a SELECT statement is re-executed.
>
> My entity graph is:
>
> Landing-------<Zone ----< ZoneWidget >--- Widget
>
> My ICriteria is...
>
> IList<Zone> zones = this.NHibernateSession.CreateCriteria(typeof
> (Zone))
>                 .CreateAlias("Landing", "l")
>                 .CreateAlias("ZoneWidgets", "zw")
>                 .CreateAlias("zw.Widget", "w")
>                 .Add(Expression.Eq("l.Line", line))
>                 .SetResultTransformer(new
> NHibernate.Transform.DistinctRootEntityResultTransformer())
>                 .AddOrder(new NHibernate.Criterion.Order("Sequence",
> true))
>                 .AddOrder(new NHibernate.Criterion.Order(string.Format
> ("zw.Sequence"), true))
>                 .List<Zone>();
>
> My logic is this...
>
> foreach(Zone zone in zones)
> {
>        // this ZoneWidgets collection association invokes another SQL
> SELECT for ZoneWidgets
>        foreach(ZoneWidget zoneWidget in zones.ZoneWidgets)
>        {
>               Widget widget = zoneWidget.Widget //doesn't cause
> another SELECT
>               string name = widget.Name //again, doesn't cause another
> SELECT
>        }
>
> }
>
> --------------------------------------------------------------------------------------------------
> Here are my mapping files (sorry for the length)
>
> [[[[[[[[[[[[[[[ Zone.hbm.xml ]]]]]]]]]]]]]
> <hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-
> mapping-2.2">
>   <class name="Zone" table="Zone" schema="dbo">
>
>     <!-- Primary Key Mapping -->
>     <id name="ID" column="ZoneID" unsaved-value="0">
>       <generator class="native" />
>     </id>
>
>     <!-- Column Mapping -->
>     <property name="Name" column="Name" />
>     <property name="Description" column="Description" />
>     <property name="Sequence" column="Sequence" />
>
>      <!-- Relationship Mapping -->
>       <many-to-one name="Landing" class="Landing" fetch="select">
>           <column name="LandingID" not-null="true" />
>       </many-to-one>
>
>       <bag name="ZoneWidgets" inverse="true" cascade="none"
> fetch="join">
>           <key>
>               <column name="ZoneID" not-null="true" />
>           </key>
>           <one-to-many class="ZoneWidget" />
>       </bag>
>   </class>
> </hibernate-mapping>
>
> [[[[[[[[[[[[[[[ ZoneWidget.hbm.xml ]]]]]]]]]]]]]
> <hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-
> mapping-2.2">
>   <class name="ZoneWidget" table="ZoneWidget" schema="dbo">
>
>     <!-- Primary Key Mapping -->
>     <id name="ID" column="ZoneWidgetID" unsaved-value="0">
>       <generator class="native" />
>     </id>
>
>     <!-- Column Mapping -->
>      <property name="Sequence" column="Sequence" />
>
>      <!-- Relationship Mapping -->
>       <many-to-one name="Widget" class="Widget" fetch="select">
>           <column name="WidgetID" not-null="true" />
>       </many-to-one>
>
>       <many-to-one name="Zone" class="Zone" fetch="select">
>           <column name="ZoneID" not-null="true" />
>       </many-to-one>
>
>       <bag name="ZoneWidgetParameters" inverse="true" cascade="none">
>           <key>
>               <column name="ZoneWidgetID" not-null="true" />
>           </key>
>           <one-to-many class="ZoneWidgetParameter" />
>       </bag>
>   </class>
> </hibernate-mapping>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to