These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: set members are returned as separate elements
PostPosted: Mon Jul 23, 2007 2:36 pm 
Newbie

Joined: Mon Jul 23, 2007 2:18 pm
Posts: 1
As a result of left join fetch rfq.parts, the query below is returning a list with 3 elements. I would expect it to return a list with 1 element, since although the generated sql correctly returns 3 rows, hibernate is normally smart enough to realize that all 3 of these parts are part of a parts set for a single rfq. Could anyone tell me why this is happening?


Hibernate version: 3.0

Mapping documents:

Code:
<hibernate-mapping package="com.blah.lbwebpo.model">

   <class name="Rfq" table="RFQ_TABLE">
      <id name="rfqId" column="RFQ_ID" type="integer">
         <generator class="native" />
      </id>

      <set name="parts" inverse="true" cascade="all" sort="natural">
         <key>
            <column name="RFQ_ID" precision="22" scale="0" not-null="true" />
         </key>
         <one-to-many class="com.blah.lbwebpo.model.RfqPart" />
      </set>
   </class>

</hibernate-mapping>


<hibernate-mapping package="com.blah.lbwebpo.model">
   <class name="RfqPart" table="RFQ_PARTS_TABLE">
      <id name="rfqPartId" type="long">
         <column name="RFQ_PART_ID" precision="22" scale="0" />
         <generator class="sequence">
            <param name="sequence">rfq_part_id_seq</param>
         </generator>
      </id>
      <property name="rfqId" type="long">
         <column name="RFQ_ID" precision="22" scale="0" />
      </property>
      <property name="partNumber" type="string">
         <column name="PART_NUMBER" length="20" />
      </property>
      <property name="partDescription" type="string">
         <column name="PART_DESCRIPTION" length="20" />
      </property>
      <property name="masterDescription" type="string">
         <column name="MASTER_DESCRIPTION" length="4000" />
      </property>
      <property name="unitMeasure" type="string">
         <column name="UNIT_MEASURE" length="2" />
      </property>
      <property name="lineItem" type="string">
         <column name="LINE_ITEM" length="11" />
      </property>
      <property name="sequentialId" type="long">
         <column name="SEQUENTIAL_ID" precision="22" scale="0" />
      </property>
      <property name="quantity" type="double">
         <column name="QUANTITY" precision="9" />
      </property>
      <property name="requisitionName" type="string">
         <column name="REQUISITION_NAME" length="128" />
      </property>
      <property name="numOtherRfqs" formula="(select count(*) from RFQ_PARTS_TABLE rpt where rpt.PART_NUMBER = PART_NUMBER AND rpt.RFQ_ID != RFQ_ID)" type="integer"/>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
List rfqs = session.createQuery("from Rfq rfq left join fetch rfq.parts where rfq.rfqId = 2701").list();
               System.out.println(rfqs.size());
System.out.println(rfqs);


Name and version of the database you are using:
Oracle 9x

The generated SQL (show_sql=true):
select
rfq0_.RFQ_ID as RFQ1_1_0_,
parts1_.RFQ_PART_ID as RFQ1_12_1_,
parts1_.RFQ_ID as RFQ2_12_1_,
parts1_.PART_NUMBER as PART3_12_1_,
parts1_.PART_DESCRIPTION as PART4_12_1_,
parts1_.MASTER_DESCRIPTION as MASTER5_12_1_,
parts1_.UNIT_MEASURE as UNIT6_12_1_,
parts1_.LINE_ITEM as LINE7_12_1_,
parts1_.SEQUENTIAL_ID as SEQUENTIAL8_12_1_,
parts1_.QUANTITY as QUANTITY12_1_,
parts1_.REQUISITION_NAME as REQUISI10_12_1_,
(select
count(*)
from
RFQ_PARTS_TABLE rpt
where
rpt.PART_NUMBER = parts1_.PART_NUMBER
AND rpt.RFQ_ID != parts1_.RFQ_ID) as formula0_1_,
parts1_.RFQ_ID as RFQ2_0__,
parts1_.RFQ_PART_ID as RFQ1_0__
from
RFQ_TABLE rfq0_
left outer join
RFQ_PARTS_TABLE parts1_
on rfq0_.RFQ_ID=parts1_.RFQ_ID
where
rfq0_.RFQ_ID=2701


Debug level Hibernate log excerpt:

13:26:44,703 WARN RootClass:215 - composite-id class does not override hashCode(): com.blah.lbwebpo.model.PoPartId
13:26:44,703 WARN RootClass:215 - composite-id class does not override hashCode(): com.blah.lbwebpo.model.ReqPartId
13:26:47,781 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/eclipse_workspace/lbwebpostruts/WebRoot/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
13:26:47,797 DEBUG Configuration$DiskStore:185 - Disk Store Path: C:\DOCUME~1\RSRINI~1\LOCALS~1\Temp\
13:26:48,609 DEBUG SQL:346 -
select
rfq0_.RFQ_ID as RFQ1_1_0_,
parts1_.RFQ_PART_ID as RFQ1_12_1_,
parts1_.RFQ_ID as RFQ2_12_1_,
parts1_.PART_NUMBER as PART3_12_1_,
parts1_.PART_DESCRIPTION as PART4_12_1_,
parts1_.MASTER_DESCRIPTION as MASTER5_12_1_,
parts1_.UNIT_MEASURE as UNIT6_12_1_,
parts1_.LINE_ITEM as LINE7_12_1_,
parts1_.SEQUENTIAL_ID as SEQUENTIAL8_12_1_,
parts1_.QUANTITY as QUANTITY12_1_,
parts1_.REQUISITION_NAME as REQUISI10_12_1_,
(select
count(*)
from
RFQ_PARTS_TABLE rpt
where
rpt.PART_NUMBER = parts1_.PART_NUMBER
AND rpt.RFQ_ID != parts1_.RFQ_ID) as formula0_1_,
parts1_.RFQ_ID as RFQ2_0__,
parts1_.RFQ_PART_ID as RFQ1_0__
from
RFQ_TABLE rfq0_
left outer join
RFQ_PARTS_TABLE parts1_
on rfq0_.RFQ_ID=parts1_.RFQ_ID
where
rfq0_.RFQ_ID=2701
13:26:49,078 DEBUG IntegerType:123 - returning '2701' as column: RFQ1_1_0_
13:26:49,078 DEBUG LongType:123 - returning '2772' as column: RFQ1_12_1_
13:26:49,078 DEBUG LongType:123 - returning '2701' as column: RFQ2_12_1_
13:26:49,078 DEBUG StringType:123 - returning 'AWE8478-17' as column: PART3_12_1_
13:26:49,078 DEBUG StringType:123 - returning 'test it' as column: PART4_12_1_
13:26:49,078 DEBUG StringType:123 - returning 'AWE8478-17 BRACKET OUTSIDE MFG
MANUFACTURE IN ACCORDANCE WITH CONTRACT ITEMS
SPECIFICATION PER NC CHANGE
APPL..DPS 1.05-2 1.05-3 3.02 3.27-1
3.301 3.301-1 3.320 3.67-22 3.69-5
4.50-36 4.710-1 4.710-2 4.710-4 9.45' as column: MASTER5_12_1_
13:26:49,078 DEBUG StringType:123 - returning 'PC' as column: UNIT6_12_1_
13:26:49,078 DEBUG StringType:116 - returning null as column: LINE7_12_1_
13:26:49,078 DEBUG LongType:123 - returning '1' as column: SEQUENTIAL8_12_1_
13:26:49,093 DEBUG DoubleType:123 - returning '0.0' as column: QUANTITY12_1_
13:26:49,093 DEBUG StringType:116 - returning null as column: REQUISI10_12_1_
13:26:49,093 DEBUG IntegerType:123 - returning '0' as column: formula0_1_
13:26:49,093 DEBUG IntegerType:123 - returning '2701' as column: RFQ2_0__
13:26:49,093 DEBUG LongType:123 - returning '2772' as column: RFQ1_0__
13:26:49,093 DEBUG IntegerType:123 - returning '2701' as column: RFQ1_1_0_
13:26:49,093 DEBUG LongType:123 - returning '2773' as column: RFQ1_12_1_
13:26:49,093 DEBUG LongType:123 - returning '2701' as column: RFQ2_12_1_
13:26:49,093 DEBUG StringType:123 - returning 'AWN7198-11' as column: PART3_12_1_
13:26:49,093 DEBUG StringType:123 - returning 'okay' as column: PART4_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'AWN7198-11 BRACKET ASSY OUTSIDE MFG
MANUFACTURE IN ACCORDANCE WITH CONTRACT ITEMS
SPECIFICATION PER 9B CHANGE
APPL..DPS 1.05-2 1.05-3 2.70-2 3.02
3.301 3.301-1 3.320 3.67-22 4.710-1
4.710-4 9.45' as column: MASTER5_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'PC' as column: UNIT6_12_1_
13:26:49,109 DEBUG StringType:116 - returning null as column: LINE7_12_1_
13:26:49,109 DEBUG LongType:123 - returning '2' as column: SEQUENTIAL8_12_1_
13:26:49,109 DEBUG DoubleType:123 - returning '5.0' as column: QUANTITY12_1_
13:26:49,109 DEBUG StringType:116 - returning null as column: REQUISI10_12_1_
13:26:49,109 DEBUG IntegerType:123 - returning '0' as column: formula0_1_
13:26:49,109 DEBUG IntegerType:123 - returning '2701' as column: RFQ2_0__
13:26:49,109 DEBUG LongType:123 - returning '2773' as column: RFQ1_0__
13:26:49,109 DEBUG IntegerType:123 - returning '2701' as column: RFQ1_1_0_
13:26:49,109 DEBUG LongType:123 - returning '2774' as column: RFQ1_12_1_
13:26:49,109 DEBUG LongType:123 - returning '2701' as column: RFQ2_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'AZZ7091-18' as column: PART3_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'abc' as column: PART4_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'AZZ7091-18 ANGLE OUTSIDE MFG
MANUFACTURE IN ACCORDANCE WITH CONTRACT ITEMS
SPECIFICATION PER A CHANGE
FISCHER
APPL..DPS 1.05-2 1.05-3 3.02 3.301
3.301-1 3.320 3.69-5 4.710-1 4.710-2
4.710-4 7.00-1 9.301 9.45' as column: MASTER5_12_1_
13:26:49,109 DEBUG StringType:123 - returning 'PC' as column: UNIT6_12_1_
13:26:49,109 DEBUG StringType:116 - returning null as column: LINE7_12_1_
13:26:49,109 DEBUG LongType:123 - returning '3' as column: SEQUENTIAL8_12_1_
13:26:49,109 DEBUG DoubleType:123 - returning '4.0' as column: QUANTITY12_1_
13:26:49,109 DEBUG StringType:116 - returning null as column: REQUISI10_12_1_
13:26:49,109 DEBUG IntegerType:123 - returning '0' as column: formula0_1_
13:26:49,109 DEBUG IntegerType:123 - returning '2701' as column: RFQ2_0__
13:26:49,109 DEBUG LongType:123 - returning '2774' as column: RFQ1_0__
3
[com.blah.lbwebpo.model.Rfq@e8a021[rfq id=2701], com.blah.lbwebpo.model.Rfq@e8a021[rfq id=2701], com.blah.lbwebpo.model.Rfq@e8a021[rfq id=2701]]


Top
 Profile  
 
 Post subject: RE: set members are returned as separate elements
PostPosted: Mon Jul 23, 2007 11:07 pm 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
To get the desired result I think the following code will work. Note the use of select and uniqueResult().

Code:
Rfq rfq = (Rfq)session.createQuery("select rfq from Rfq rfq left join fetch rfq.parts where rfq.rfqId = 2701").list().uniqueResult();


I cannot tell you why this is but I can tell you that the behavior you observed is documented. I found this in the Hibernate documentation:

Quote:
A query is usually executed by invoking list(), the result of the query will be loaded completely into a collection in memory. Entity instances retrieved by a query are in persistent state. The uniqueResult() method offers a shortcut if you know your query will only return a single object. Note that queries that make use of eager fetching of collections usually return duplicates of the root objects (but with their collections initialized). You can filter these duplicates simply through a Set.


The url to this is: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-querying-executing

I hope that helps.

Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.