File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Eager Fetching a Polymorphic SET Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Eager Fetching a Polymorphic SET " Watch "Eager Fetching a Polymorphic SET " New topic
Author

Eager Fetching a Polymorphic SET

vds mahendra
Greenhorn

Joined: Mar 11, 2009
Posts: 1
Using Hibernate 3.2.2

I have an Entity class with a set<Relation> (Relation is a subclass of Entity).

Given below is the class def. and its mapping.

abstract class Entity {
Set<Relation> relationOrderSet;
..
}
class Relation extends Entity {
Entity endpoint0;
Entity endpoint1;
...
}
Mapping: table per class hierarchy

<class name="Entity" table="ENTITY" abstract="true">
<id name="entityGlobalId" type="string" column="entity_global_id">
<generator class="assigned"></generator>
</id>
<discriminator column="E" type="string" force="true"></discriminator>
<set name="relationOrderSet" table="relation_order_set" fetch="join">
<key column="entity_global_id"></key>
<many-to-many class="Relation"></many-to-many>
</set>
<subclass name="Relation" discriminator-value="R">
<many-to-one name="endpoint0" class="Entity" lazy="false" cascade="all" > </many-to-one>
<many-to-one name="endpoint1" class="Entity" lazy="false" cascade="all" > </many-to-one>
</subclass>
</class>

When I try to do entity.getRelations( ) on the <set> hibernate issues n+1 selects for each entity in the relation.
I can clearly see that it happens because of <many-to-one> properties mapped as lazy="false".
If i take off lazy="false" with fetch='join' then there is just 1 select and it initializes the complete set BUT with proxies. Which are soon needed and
hence more selects coming back to same n+1 problem

I am looking to eager fetch all the entities(elements) of a set for a given entity.
Something like: "from entity e left join fetch e.relationOrderSet ros where e.id = :id"
If i don't put the 'where' clause all the entities with all the relations gets loaded and that would be a nightmare with millions of entities.
If i put the where clause then Hibernate issues n + 1 selects. (for the above mapping)

I want to batch-select only the entities within the set (with No Proxies) for a given entity

Thanks all

vds
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Eager Fetching a Polymorphic SET