2010-08-25 73 views
0

我們有一對多的關係,我們試圖在NHibernate中實現。這是my colleague's question的改寫。爲什麼NHibernate無法延遲加載取決於SetMaxResults參數?

GroupPartnerInterests每一個都具有公司的集合。以下測試方法通過SetMaxResults(3)但失敗SetMaxResults(5)。例外的是

NHibernate.LazyInitializationException: 初始化[Model.EntityClasses.BaseBlock#100000121437] -failed 懶洋洋地初始化的 角色的集合: Model.EntityClasses.BaseBlock.GroupPartnerInterests, 沒有會話或會議關閉。

問題是爲什麼SetMaxResults的論點很重要?

的測試方法是:

[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); 
       } 
      } 
     } 
    } 
} 

配置個XML:

<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> 
+0

更新您的文章與你的類實現,還有一些捕獲的SQL痕跡(使用類似NHProf)會給你額外的背景下,可以證明在故障排除這是很有幫助的。 – DanP 2010-08-26 09:52:33

回答

1

我不能肯定,如果這是在你的問題小號但是總的來說,當使用Nhibernate作爲discussed here時,不鼓勵使用隱式交易。您的數據訪問應該始終遵循這種模式:

using(var session = sessionFactory.OpenSession()) 
using(var tx = session.BeginTransaction()) 
{ 
    // execute code that uses the session 
    tx.Commit(); 
} 
+0

謝謝。但問題依然存在。一切都使用'SetMaxResults(3)'運行,但使用'SetMaxResults(5)'失敗。 – zzandy 2010-08-26 08:30:07