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!