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