Hibernate version:3.05
Is there a way for me to execute an HQL query and have the
fetch attibute specified for an association in the mapping file be picked up without me having to explicitly specify
left outer join in my HQL query?
It seems like the
fetch attibute of a many-to-one association is used only when loading the entity directly but not when an HQL is run where the join is implicit.
Consider the mapping below where the fetch type is outer join:
Mapping documents:
Code:
<class name="foo.Lane" table="lane">
<id name="id" column="lane_id" type="long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">lane_sequence</param>
</generator>
</id>
...
<many-to-one name="businessUnit" column="businessunit_id" fetch="join"
class="foo.BusinessUnit" />
</class>
When I run
Code:
session.load(Lane.class, new Long(1))
the generated SQL does have an outer join with the BusinessUnit table
However when I run
Code:
session.createQuery("from Lane l where l.businessUnit.name is null").list()
the generated SQL does not have the outer join. In the HQL above, the where conditional works correctly only when an outer join is executed.
If I run the following by explicitly adding the join criteria in the HQL, it works.
Code:
session.createQuery("from Lane l left join fetch l.businessUnit where l.businessUnit.name is null").list()
Unfortunaltey I cant use the latter version as I am dynamically building a filter and at the point of execution of the code I only have knowledge of the entity in question (Lane) and the properties as Strings ("businessUnit.name") and the filter value ( null, not null, *foo* etc).
Thanks,
Sanjiv