-->
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: Single SELECT to load collection including its children
PostPosted: Wed Jun 27, 2012 7:35 am 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
Hibernate 3.2

Hibernate mappings: Item contains a Set of Field objects, with each Field containing a List<String> populated from a table

Code:
   
   <class name="Field" entity-name="Field" table="item_fields" lazy="true">
      <composite-id>
         <key-property name="field_id" column="field_id"/>
         <key-property name="item_id" column="item_id"/>
      </composite-id>
      
      <list name="values" table="item_field_values" cascade="all" fetch="join">
          <key>
             <column name="field_id"/>
             <column name="item_id"/>
          </key>
             
          <list-index column="position"/>
             <element type="string" column="field_value" not-null="false"/>
          </list>
      <property name="name" column="field_name" type="string" not-null="true"/>
   </class>

    <class name="Item" table="items" lazy="true">
          <id name="item_id" type="long">
         <column name="item_id" />
         <generator class="assigned" />
           </id>
      <set name="itemData" inverse="true" cascade="all" lazy="false" fetch="join">
         <key column="item_id" on-delete="cascade"/>
         <one-to-many entity-name="Field"/>
      </set>
           <property name="item_guid" column="item_guid" type="string" not-null="true" length="36" />
           <property name="item_name" column="item_name" type="string" not-null="true"/>
   </class>


When using the get() method to return a single Item object it uses a single SELECT using JOINs to return all the Item, Field and values:

Code:
session.get(Item.class, 1L, LockMode.READ);


However, changing the LockMode.UPGRADE results in a SELECT for the Item object with UPDLOCK, which I would expect. But then it uses a SELECT to get all the Field rows and for each field row found it then does a SELECT for the values for each field. How can I get it to perform a JOIN when loading the fields so that the values are also loaded?

Quote:
Hibernate: select item0_.item_id as item1_2_0_, item0_.item_guid as item2_2_0_, item0_.item_name as item3_2_0_ from items item0_ with (updlock, rowlock) where item0_.item_id=?

Hibernate: select itemdata0_.item_id as item2_1_, itemdata0_.field_id as field1_1_, itemdata0_.field_id as field1_0_0_, itemdata0_.item_id as item2_0_0_, itemdata0_.field_name as field3_0_0_ from item_fields itemdata0_ where itemdata0_.item_id=?

Field#3
Hibernate: select values0_.field_id as field1_0_, values0_.item_id as item2_0_, values0_.field_value as field3_0_, values0_.position as position0_ from item_field_values values0_ where values0_.field_id=? and values0_.item_id=?
Value#3#1
Value#3#2

Field#1
Hibernate: select values0_.field_id as field1_0_, values0_.item_id as item2_0_, values0_.field_value as field3_0_, values0_.position as position0_ from item_field_values values0_ where values0_.field_id=? and values0_.item_id=?
Value#1#1
Value#1#2
Field#2
Hibernate: select values0_.field_id as field1_0_, values0_.item_id as item2_0_, values0_.field_value as field3_0_, values0_.position as position0_ from item_field_values values0_ where values0_.field_id=? and values0_.item_id=?
Value#2#1
Value#2#2
Value#2#3


Top
 Profile  
 
 Post subject: Re: Single SELECT to load collection including its children
PostPosted: Wed Jun 27, 2012 1:52 pm 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
It seems that the simplest way is to run a query after the session.get() call returns. This loaded all the fields and values into the object.
Code:
   Query q = session.createQuery("select a from Field a join fetch a.values where a.item_id = :id ORDER BY a.field_id, position");
   q.setLong("id", 1).list();


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.