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