Why Nhibernate won't lazy load my many-to-one relationship?

1,457 views
Skip to first unread message

Hoang Tang

unread,
Nov 25, 2010, 12:43:18 AM11/25/10
to nhu...@googlegroups.com
I have three User, Invoice and Enrollment entities

One Invoice can have many enrollments, One user can have many invoices/enrollment

so I am doing something very basic is getting a list of all Enrollments like this:

session.CreateQuery("From Enrollment").List<Enrollment>();

watching inside NHProf, I see that it's generate a select for every Invoice as well.

I am using Nhibernate 3.00.2002
Bellow is all the mapping necessary to reproduce the problem.

 For Enrollment

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Domain.Enrollment, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Enrollments">
    <id access="backfield" name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="00000000-0000-0000-0000-000000000000">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <property name="IsFinished" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="IsFinished" />
    </property>
    <many-to-one cascade="save-update" class="Domain.User, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="User" not-found="ignore">
      <column name="UserFk" />
    </many-to-one>
    <many-to-one cascade="save-update" class="Domain.Invoice, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Invoice" not-found="ignore">
      <column name="InvoiceFk" />
    </many-to-one>
  </class>
</hibernate-mapping>


for Invoice

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Domain.Invoice, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Invoices">
    <id access="backfield" name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="00000000-0000-0000-0000-000000000000">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <bag batch-size="30" cascade="save-update" inverse="true" name="Enrollments" mutable="true">
      <key>
        <column name="InvoiceFk" />
      </key>
      <one-to-many class="Domain.Enrollment, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
    <property name="InvoiceNumber" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="InvoiceNumber" />
    </property>
    <many-to-one cascade="save-update" class="Domain.User, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="User" not-found="ignore">
      <column name="UserFk" />
    </many-to-one>
  </class>
</hibernate-mapping>

for user

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Domain.User, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Users">
    <id access="backfield" name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="00000000-0000-0000-0000-000000000000">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <bag batch-size="30" cascade="save-update" inverse="true" name="Enrollments" mutable="true">
      <key>
        <column name="UserFk" />
      </key>
      <one-to-many class="Domain.Enrollment, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
    <bag batch-size="30" cascade="save-update" inverse="true" name="Invoives" mutable="true">
      <key>
        <column name="UserFk" />
      </key>
      <one-to-many class="Domain.Invoice, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
    <property name="UserName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="UserName" />
    </property>
    <property name="Password" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Password" />
    </property>
  </class>
</hibernate-mapping>





José F. Romaniello

unread,
Nov 25, 2010, 6:56:51 AM11/25/10
to nhu...@googlegroups.com
2010/11/25 Hoang Tang <firef...@gmail.com>
not-found="ignore"


This is your problem, nhibernate must to be sure, that an invoice EXIST or do not exist for each enrollment.

Id strongly recommend you to fix your data problems and remove the not-found="ignore" attribute. It is a bad thing.

Hoang Tang

unread,
Nov 25, 2010, 1:10:35 PM11/25/10
to nhu...@googlegroups.com

That make sense... the data problem has been fixed... though i didnt know the implication of using not found ignore...

Thanks for the tip Jose

> --
> You received this message because you are subscribed to the Google Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
>

Hoang Tang

unread,
Nov 25, 2010, 1:19:07 PM11/25/10
to nhu...@googlegroups.com
Now I know the culprit here is a reference after a quick google search

http://opensource.atlassian.com/projects/hibernate/browse/HHH-2753
Reply all
Reply to author
Forward
0 new messages