-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many joins to load an association of polymorphic entities
PostPosted: Fri Mar 30, 2007 6:48 pm 
Newbie

Joined: Fri Mar 30, 2007 6:26 pm
Posts: 1
Hibernate version: 3.2.0.ga

I can't post my exact hbm/java here since it's proprietary. But here is a summary:

Filter.hbm.xml :

<class name="Filter" ...>
<id...>
<list name="filterExpressions" ...>
<key ...>
<list-index ...>
<one-to-many class="....FilterExpression"/>
</list>

FitlerExpression.hbm.xml :

<class name="FilterExpression" table="filter_expressions">
<id...>
<subclass name="FooFilterValue" discriminator-value="Foo">
<list name="values" ... table="filter_expression_values">
<key...>
<list-index...>
<element ...>
</list>
</subclass>
<subclass name="BarFilterValue" discriminator-value="Bar">
<list name="values" ... table="filter_expression_values">
<key...>
<list-index...>
<element ...>
</list>
</subclass>
<subclass name="BazFilterValue" discriminator-value="Baz">
<list name="values" ... table="filter_expression_values">
<key...>
<list-index...>
<element ...>
</list>
</subclass>
</class>

The PK of filters table is filter_id. filter_expression_values.filter_id is an FK on filter.filter_id.

So, in a nutshell, a filter (in filters table) has many filter expressions (filter_expressions), each of which has many filter expression values, of various classes, but all residing in the filter_expression_values table, discriminated by the expression_type column.

Anyway, the following code..

getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException ,SQLException {
return session.createCriteria(FilterExpression.class)
.setFetchMode("values", FetchMode.JOIN)
.add(Restrictions.sqlRestriction("this_.filter_id in (select filter_id from ...)")
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
}
});

..generates code that outer joins on the "filter_expression_value" table 3 times, one per subclass. This doesn't seem necessary ... I can see why Hibernate might naively do it, but it doesn't seem necessary.

Is this just the way Hibernate works, or am I possibly doing something wrong? Is there a known work around pattern for this?

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.