简体   繁体   中英

NHIbernate 1.2 And Lazy Loading

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.

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 avoiding join fetch .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM