Hello,
Is there a way to initialize a map without initializing the key objects?
For example,
I have the following three tables.
table item (
item_id int
)
table item_property (
item_id int,
property_id int,
property_string_value,
property_int_value,
property_date_value
)
table property (
property_id int,
property_name
)
my mappings are as followed.
Item.hbm.xml
<class name="Item" table="item">
<id name="id" column="item_id">
<generator class="native" />
</id>
<map name="properties" table="item_property" cascade="all">
<key column="item_id"></key>
<index-many-to-many class="Property" column="property_id"></index-many-to-many>
<composite-element class=PropertyValue">
<property name="stringValue" column="property_string_value"></property>
<property name="dateValue" column="property_date_value"></property>
<property name="intValue" column="property_int_value"></property></composite-element>
</map>
</class>
Property.hbm.xml
<class name=Property table="property">
<id name="id" column="property_id">
<generator class="native">
</id>
<property name="name" column="name">
</class>
My java classes are as followed.
class Item {
private int id;
private Map<Property, PropertyValue> properties;
}
class Property {
private int id;
private String name;
}
class PropertyValue {
private int intValue;
private String stringValue;
private Date dateValue;
}
Now, I would like to use Item->properties map outside of a session.
So I need to eager fetch it.
getSession().createQuery("from Item i " +
"left join fetch i.properties p " +
"where i.id = :id").uniqueResult();
This query generates
Hibernate: select item0_.item_id as item1_14_, properties1_.item_id as item1_0__, properties1_.property_string_value as property2_0__, properties1_.property_date_value as property3_0__, properties1_.property_int_value as property4_0__, properties1_.property_id as property5_0__ from item item0_ left outer join item_property properties1_ on item0_.item_id=properties1_.item_id where item0_.item_id=?
Hibernate: select property0_.property_id as property1_20_0_, property0_.name as name20_0_ from property property0_ where property0_.property_id=?
Hibernate: select property0_.property_id as property1_20_0_, property0_.name as name20_0_ from property property0_ where property0_.property_id=?
Hibernate: select property0_.property_id as property1_20_0_, property0_.name as name20_0_ from property property0_ where property0_.property_id=?
Hibernate: select property0_.property_id as property1_20_0_, property0_.name as name20_0_ from property property0_ where property0_.property_id=?
....
....
It is querying each property using property_id, initializing property objects.
1) Is there a way to generate the first query line without triggering the rest? I only need
Hibernate: select item0_.item_id as item1_14_, properties1_.item_id as item1_0__, properties1_.property_string_value as property2_0__, properties1_.property_date_value as property3_0__, properties1_.property_int_value as property4_0__, properties1_.property_id as property5_0__ from item item0_ left outer join item_property properties1_ on item0_.item_id=properties1_.item_id where item0_.item_id=?
2) If not, is there a way to tell hibernate to join all three tables in one query and transform the results into one Item object?
Thank you very much in advance!
|