Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Eager Fetching a Polymorphic SET  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = :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

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!