NHIbernate 1.2 和延迟加载
遇到了一个不寻常的问题 - 我确信我现在缺少一些非常简单的东西! 特别有两个表:
<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
除非我要疯了,鉴于提案是与 ProposalData 的一对多,NH 应该加载每个建议对象和每个对象的数据作为一个集合。 不幸的是,我最终得到了重复的结果,因为每个提案都有多个提案数据。
我的理解是这不应该是一个问题。如果 ProposalData 与另一个表存在一对多关系,则将产生笛卡尔积,并且可以预期上述结果。
我错了吗?任何人都可以阐明吗?
谢谢。
Got a bit of an unusual problem - i'm sure i'm missing something really simple now!
Have two tables in particular:
<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>
and
<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>
I think have a named query in my .HBM.XML file as below:
FROM Proposal MSP
JOIN FETCH MSP.DataItems Items
Unless i'm going nutes, given that the Proposal is a one-to-many with ProposalData, NH should load each of the Proposal objects, and the Data for each as a collection.
Unfortunately, i'm ending up with duplicate results, as there are multiple ProposalData for each Proposal.
My understand is this should not be a problem. If ProposalData had a one-to-many with another table, then a Cartesian product would result and the above could be expected.
Am i incorrect? Can anyone shed any light?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JOIN FETCH
表示项目已连接并用于获取它们。这导致提案数量增加。注意:重复项仍然是内存中的相同实例。通过使用
DistinctRootEntityTransformer
或避免join fetch
来修复此问题。JOIN FETCH
means that the items are joined and also used to fetch them. This leads to multiplication of the proposals. Note: the duplicates are still the same instances in memory.Fix it by using the
DistinctRootEntityTransformer
or by avoidingjoin fetch
.