-->
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.  [ 1 post ] 
Author Message
 Post subject: fetch=join Object Graph Loaded Wrong Yet fetch=select Works
PostPosted: Wed Mar 19, 2008 8:15 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
I am attempting to load a ScheduledContentPackage . Its parent, ContentPackage, has a collection of Assets. These assets are not loaded as expected with fetch="join". Yet they are with fetch="select".

Instead of adding each Asset to ContentPackage's Asset collection, Hibernate creates N instances of ScheduledContentPackage,
where N is the number of child Assets joined onto ContentPackage.

Using MySql

Hibernate version:
3

Mapping documents:
Irrelevant properties removed.
Code:
    <class name="ScheduledContentPackage" table="ScheduledPackage">

      <meta attribute="use-in-tostring">true</meta>
      <id name="id" type="long">
   <generator class="native"/>
      </id>         

      <many-to-one name="contentPackage" class="ContentPackage" not-null="true" fetch="join">
        <meta attribute="use-in-equals">true</meta>
      </many-to-one>

    </class>



Code:

<class name="ContentPackage" table="Package">

 
      <id name="id" type="long">
   <meta attribute="use-in-tostring">true</meta>
   <generator class="native"/>
      </id>         

     
     
      <bag name="schedule" inverse="true" order-by="deliveryDate" cascade="all-delete-orphan">
   <key column="contentPackage"/>
   <one-to-many  class="ScheduledContentPackage"/>
      </bag>

     <!-- ====================================
   this is the entity causing a problem
     ======================================== -->

     <map name="assets" inverse="true" cascade="all-delete-orphan" fetch="join"> 
   <meta attribute="use-in-tostring">true</meta>
   <meta attribute="property-type">java.util.Map</meta>
   <key column="contentPackage"/>
   <map-key column="type" type="short"/>
   <one-to-many class="Asset"/>
      </map>


    </class>


Code:
    <class name="Asset" table="Asset">

      <id name="id" type="long">
   <meta attribute="use-in-tostring">true</meta>
   <generator class="native"/>
      </id>         
     
      <many-to-one name="contentPackage" class="ContentPackage" not-null="true">
   <meta attribute="use-in-equals">true</meta>
      </many-to-one>

      <many-to-one name="type" class="AssetType" not-null="true">
   <meta attribute="use-in-equals">true</meta>
   <meta attribute="use-in-tostring">true</meta>
      </many-to-one>

    </class>


Code between sessionFactory.openSession() and session.close():
Code:
Criteria c = getSession().createCriteria(getPersistentClass())
       .add(Restrictions.between("deliveryDate",start,end))
       .createCriteria("contentPackage")
       .createCriteria("service")
       .createCriteria("provider")
       .add(Restrictions.eq("id",providerId)).list();


The generated SQL (show_sql=true):
Code:
select
     this_.id as id2_4_,
     this_.deliveryStatus as delivery2_2_4_,
     this_.notes as notes2_4_,
     this_.deliveryDate as delivery4_2_4_,
     this_.contentPackage as contentP5_2_4_,
     this_.recipient as recipient2_4_,
     contentpac1_.id as id7_0_,
     contentpac1_.name as name7_0_,
     contentpac1_.qcStatus as qcStatus7_0_,
     contentpac1_.validationStatus as validati4_7_0_,
     contentpac1_.runtime as runtime7_0_,
     contentpac1_.service as service7_0_,
     assets6_.contentPackage as contentP5_6_,
     assets6_.id as id6_,
     assets6_.type as type6_,
     assets6_.id as id8_1_,
     assets6_.name as name8_1_,
     assets6_.dateAdded as dateAdded8_1_,
     assets6_.content as content8_1_,
     assets6_.contentPackage as contentP5_8_1_,
     assets6_.type as type8_1_,
     service2_.id as id0_2_,
     service2_.provider as provider0_2_,
     service2_.product as product0_2_,
     provider3_.id as id12_3_,
     provider3_.name as name12_3_,
     provider3_.URL as URL12_3_,
     provider3_.contentProvider as contentP4_12_3_
from
     ScheduledPackage this_
inner join
     Package contentpac1_
         on this_.contentPackage=contentpac1_.id
left outer join
     Asset assets6_
         on contentpac1_.id=assets6_.contentPackage
inner join
     Service service2_
         on contentpac1_.service=service2_.id
inner join
     Provider provider3_
         on service2_.provider=provider3_.id
where
     this_.deliveryDate=?
     and provider3_.id=?


Thanks!


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

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.