views:

38

answers:

1

Hi, we have one to many to one relationship which we are trying to implement in NHibernate. This is a rephrase of my colleague's question.

There is Block with a collection of GroupPartnerInterests every of which has a Company. Following test method passes with SetMaxResults(3) but fails with SetMaxResults(5). Exception is

NHibernate.LazyInitializationException: Initializing[Model.EntityClasses.BaseBlock#100000121437]-failed to lazily initialize a collection of role: Model.EntityClasses.BaseBlock.GroupPartnerInterests, no session or session was closed.

Question is why does SetMaxResults's argument matter?

The test method is:

[TestMethod]
public void TestGroupPartnerInterests()
{
    using ( ISession session = SessionFactory.OpenSession() )
    {
        IList<Block> blocks = session
            .CreateCriteria( typeof( Block ) )
            .SetMaxResults( 5 ).List<Block>();

        foreach ( var block in blocks )
        {
            TestContext.WriteLine( block.BlockId + " " + block.BlockName );

            if ( block.GroupPartnerInterests != null )
            {
                foreach ( var gpi in block.GroupPartnerInterests )
                {
                    TestContext.WriteLine( gpi.Company.CompanyName );
                }
            }
        }
    }
}

Configuration XMLs:

<class name="Block" table="[BLOCK]">
  <id name="BlockId" column="GA_ID" access="field.camelcase-underscore" >
    <generator class="assigned"/>
  </id>
  ... data properties ...
  <many-to-one name="Contract" access="field.camelcase-underscore" 
               fetch="select" unique="true">
    <column name="EPC_ID"/>
  </many-to-one>
  <many-to-one name="Country" access="field.camelcase-underscore" 
               fetch="select" cascade="none">
    <column name="COUNTRY_ID"/>
  </many-to-one>

  <set name="GroupPartnerInterests" access="field.camelcase-underscore"
       cascade="all-delete-orphan" fetch="select">
    <key property-ref="GroupId">
      <column name="PAR_ID"/>
    </key>
    <one-to-many class="GroupPartnerInterest"/>
  </set>
</class>

<class name="GroupPartnerInterest" table="[GROUP_PARTNER_INTERESTS]">
  <composite-id >
    <key-property name="GroupId" column="PAR_ID" />
    <key-property name="CompanyId" column="PU_ID" />
  </composite-id>
  ... data properties ...
  <many-to-one name="Company" access="field.camelcase-underscore" 
               fetch="select" cascade="none">
    <column name="PU_ID"/>
  </many-to-one>
</class>

<class name="Company" table="[COMPANY]">
  <id name="CompanyId" column="PU_ID" access="field.camelcase-underscore" >
    <generator class="assigned"/>
  </id>
  ... data properties ...
  <set name="GroupPartnerInterests" access="field.camelcase-underscore" 
       cascade="all-delete-orphan" inverse="true" fetch="select">
    <key>
      <column name="PU_ID"/>
    </key>
    <one-to-many class="GroupPartnerInterest"/>
  </set>
</class>
+1  A: 

I'm not certain if this is the problem in your specific case, but in general, the use of implicit transactions is discouraged when using Nhibernate as discussed here. Your data access should always follow this pattern:

using(var session = sessionFactory.OpenSession()) 
using(var tx = session.BeginTransaction()) 
{ 
    // execute code that uses the session 
    tx.Commit(); 
} 
DanP
Thank you. But the issue remains. Everything runs with `SetMaxResults(3)` but fails with `SetMaxResults(5)`.
zzandy