Hibernate version: 2.1.6
Mapping documents: Amenity.xml
Name and version of the database you are using: postgresql
The generated SQL (show_sql=true): true
Debug level Hibernate log excerpt: debug
Hello,
Thanks in advance for your help, i have the following mapping: on the same table i have a parent child relationship and a component mapping in a map indexed by a string code
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="com.ec.db.hibernate.Amenity" table="amenity" lazy="true">
<id name="idAmenity" type="java.lang.Integer" column="id_amenity" unsaved-value="null">
<generator class="sequence">
<param name="sequence">seq_amenity</param>
</generator>
</id>
<!-- bi-directional one-to-many association to AmenitiesDetail -->
<map name="amenityDetails" table="amenity_details" outer-join="true">
<key column="id_amenity_ref"/>
<index column="id_lang_ref" type="string"/>
<composite-element class="com.ec.db.hibernate.AmenityDetails">
<property name="name" column="name" type="string" />
<property name="description" column="description" type="string"/>
</composite-element>
</map>
<many-to-one name="parent" class="com.ec.db.hibernate.Amenity"
foreign-key="parent"/>
<set name="childs" lazy="true" >
<key>
<column name="parent" />
</key>
<one-to-many class="com.ec.db.hibernate.Amenity" />
</set>
</class>
</hibernate-mapping>
My problem is that amenityDetails is not eagerly fetched for the childrens when i load an amenity using load/get and then get the childrens.
For the first amnity i get eager fetching but not for childrens.
The same happens if i do HQL query:
Code:
"select am from Amenity as am left join fetch am.amenityDetails as ad where am.parent is null order by am.idAmenity"
all the parent get fetched and the amenityDetails get fetched eagerly with a join. But if i call:
Code:
Set chidlrens = amenity.getChildrens();
The amenityDetails won't get eagerly fetched for the childrens, resulting in a big number of queries:
Quote:
(number of children per parent) * (number of parent)
and i would like only
Quote:
1 + (number of parents)
queries
Here i show you an example code:
Code:
Session s = HibernateUtil.currentSession();
amenities = s.createCriteria(Amenity.class).add(Expression.isNull("parent")).list();
amenitiesIt = amenities.iterator();
while (amenitiesIt.hasNext()) {
parent = (Amenity)amenitiesIt.next();
log.info("found parent " + parent.getAmenityDetails().get("fr"));
//getting all childrens
Set childs = parent.getChilds();
for (Iterator ci = childs.iterator(); ci.hasNext();) {
Amenity child = (Amenity)ci.next();
AmenityDetails ad = (AmenityDetails)child.getAmenityDetails().get("fr");
log.debug(" amenity details for that child " + ad.getName());
}
//query.remove(amenity);
}
log.info("retrieving amenity and cleaning");
and here is the output:
Code:
Hibernate: select this.id_amenity as id_amenity0_, this.parent as parent0_, amenitydet1_.id_amenity_ref as id_ameni1___, amenitydet1_.name as name__, amenitydet1_.description as descript3___, amenitydet1_.id_lang_ref as id_lang_4___ from amenity this left outer join amenity_details amenitydet1_ on this.id_amenity=amenitydet1_.id_amenity_ref where this.parent is null
13:05:59,991 INFO ConnectionTest:279 - found parent com.ec.db.hibernate.AmenityDetails@575c7e[]
Hibernate: select childs0_.parent as parent__, childs0_.id_amenity as id_amenity__, childs0_.id_amenity as id_amenity0_, childs0_.parent as parent0_ from amenity childs0_ where childs0_.parent=?
Hibernate: select amenitydet0_.id_amenity_ref as id_ameni1___, amenitydet0_.name as name__, amenitydet0_.description as descript3___, amenitydet0_.id_lang_ref as id_lang_4___ from amenity_details amenitydet0_ where amenitydet0_.id_amenity_ref=?
Hibernate: select amenitydet0_.id_amenity_ref as id_ameni1___, amenitydet0_.name as name__, amenitydet0_.description as descript3___, amenitydet0_.id_lang_ref as id_lang_4___ from amenity_details amenitydet0_ where amenitydet0_.id_amenity_ref=?
Hibernate: select amenitydet0_.id_amenity_ref as id_ameni1___, amenitydet0_.name as name__, amenitydet0_.description as descript3___, amenitydet0_.id_lang_ref as id_lang_4___ from amenity_details amenitydet0_ where amenitydet0_.id_amenity_ref=?
13:06:00,096 DEBUG ConnectionTest:285 - amenity details for that child test1_Français
13:06:00,097 DEBUG ConnectionTest:285 - amenity details for that child test3_Français
13:06:00,098 DEBUG ConnectionTest:285 - amenity details for that child test2_Français
13:06:00,099 INFO ConnectionTest:279 - found parent com.ec.db.hibernate.AmenityDetails@575c7e[]
13:06:00,100 DEBUG ConnectionTest:285 - amenity details for that child test1_Français
13:06:00,102 DEBUG ConnectionTest:285 - amenity details for that child test3_Français
13:06:00,103 DEBUG ConnectionTest:285 - amenity details for that child test2_Français
13:06:00,104 INFO ConnectionTest:279 - found parent com.ec.db.hibernate.AmenityDetails@575c7e[]
13:06:00,105 DEBUG ConnectionTest:285 - amenity details for that child test1_Français
13:06:00,111 DEBUG ConnectionTest:285 - amenity details for that child test3_Français
13:06:00,112 DEBUG ConnectionTest:285 - amenity details for that child test2_Français
13:06:00,116 INFO ConnectionTest:279 - found parent com.ec.db.hibernate.AmenityDetails@575c7e[]
13:06:00,117 DEBUG ConnectionTest:285 - amenity details for that child test1_Français
13:06:00,118 DEBUG ConnectionTest:285 - amenity details for that child test3_Français
13:06:00,175 DEBUG ConnectionTest:285 - amenity details for that child test2_Français
13:06:00,185 INFO ConnectionTest:279 - found parent com.ec.db.hibernate.AmenityDetails@575c7e[]
13:06:00,187 DEBUG ConnectionTest:285 - amenity details for that child test1_Français
13:06:00,188 DEBUG ConnectionTest:285 - amenity details for that child test3_Français
13:06:00,190 DEBUG ConnectionTest:285 - amenity details for that child test2_Français
Max fetch depth is set to 4 and outer-join to true..
I would like to know if this is a limitation, a bug or a misconfiguration. I have read the book hibernate in action but couldn't find any answers.
Thanks in advance.
Numito