0

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.

TheITGuy
  • 722
  • 4
  • 15
  • Ahhhhhhhhhhh! The Proposal is returning many results, as is the ProposalData for each Proposal, hence a cartesian product is produced, causing NH to load data as i am experiencing above. – TheITGuy Aug 02 '11 at 05:51

1 Answers1

1

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.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • Hi Stefan - the FETCH essentially eagerly loads the ProposalData right? Is there a NH way to print the Proposals in this case without having to hit the database for each to retrieve each ProposalData? – TheITGuy Aug 02 '11 at 05:54
  • Thanks. If i avoid JOIN FETCH however, i cannot retrieve the ProposalData items associated to Proposal though? (at least not eagerly?) – TheITGuy Aug 02 '11 at 06:07