【问题标题】:NHIbernate 1.2 And Lazy LoadingNHIbernate 1.2 和延迟加载
【发布时间】:2011-10-17 23:26:09
【问题描述】:

遇到了一个不寻常的问题 - 我确定我现在遗漏了一些非常简单的东西! 特别有两张表:

   <class name="Proposal" table="Proposal">
      <id name="Id" column="ProposalId">
         <generator class="identity" />
      </id>

      <property name="QuotationNumber" column="QuotationNumber" access="nosetter.camelcase-underscore" />

      <set name="DataItems" table="ProposalData" inverse="true" cascade="save-update" access="nosetter.camelcase-underscore" lazy="true">
         <key column="ProposalId" />
         <one-to-many class="Fortron.Fastr.Domain.Proposal.ProposalData, Fortron.Fastr.Domain"/>
      </set>
   </class>

和

   <class name="ProposalData" table="ProposalData">
      <id name="Id" column="ProposalDataId">
         <generator class="identity" />
      </id>
      <many-to-one name="Proposal" column="ProposalId" class="Fortron.Fastr.Domain.Proposal.Proposal, Fortron.Fastr.Domain" />

   </class>

我认为我的 .HBM.XML 文件中有一个命名查询,如下所示:

  FROM Proposal MSP
  JOIN FETCH MSP.DataItems Items

除非我要疯了,否则考虑到提案与提案数据是一对多的,NH 应该加载每个提案对象,并将每个提案的数据作为一个集合加载。 不幸的是,我得到了重复的结果,因为每个提案都有多个 ProposalData。

我的理解是这应该不是问题。如果 ProposalData 与另一个表是一对多的,那么将产生笛卡尔积,并且可以预期上述情况。

我错了吗?任何人都可以解释一下吗?

谢谢。

【问题讨论】:

  • 啊啊啊啊啊!提案返回了许多结果,每个提案的 ProposalData 也是如此,因此产生了笛卡尔积,导致 NH 加载数据,就像我在上面遇到的那样。

标签: c# nhibernate loading lazy-evaluation cartesian


【解决方案1】:

JOIN FETCH 表示项目已连接并用于获取它们。这导致提案的成倍增加。注意:重复的仍然是内存中的相同实例。

使用DistinctRootEntityTransformer 或避免使用join fetch 来修复它。

【讨论】:

  • 嗨 Stefan - FETCH 本质上是急切地加载 ProposalData 对吧?在这种情况下,是否有一种 NH 方法可以打印提案,而不必为每个数据库访问每个 ProposalData 来检索每个 ProposalData?
  • 谢谢。但是,如果我避免 JOIN FETCH,我无法检索与 Proposal 关联的 ProposalData 项目吗? (至少不急切?)
猜你喜欢
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 2013-12-10
  • 1970-01-01
相关资源
最近更新 更多