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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Eager Fetching a Polymorphic SET " Watch "Eager Fetching a Polymorphic SET " New topic

Eager Fetching a Polymorphic SET

vds mahendra

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>
<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>
<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>

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

I agree. Here's the link: http://aspose.com/file-tools
subject: Eager Fetching a Polymorphic SET
Similar Threads
Hibernate: multiple subclasses not using fetchmode join
Hibernate- one-to-many association on a foreign key
Fetch=join not eager fetching collections
does Hibernate support one-to-one bidirectional in subclass?
Parent class id value instead of child's id used incorrectly for querying Set assoc.