Have a question regarding results I'm observing for an inheritance mapping (using the hierarchy in a single table approach, Hibernate version 2.1.6). With the mapping below for a Closeout, I am able to successfully retrieve subclass instances via HQL. However, when I try to load them via one-to-many assocation from another object, the discriminator value for the subclass seems to be ignored and Hibernate appears to retrieve all instances of the parent class.
Here's the mapping:
Code:
<hibernate-mapping package="com.nlg.nlgv.paris.model">
<class name="Restriction" table="calendar_rule_vw" discriminator-value="0">
<id name="id" column="rule_id">
<generator class="native"/>
</id>
<discriminator column="predicate_type_id" type="integer"/>
<property name="targetId" column="rule_target_id"/>
<property name="description" column="rule_desc"/>
<property name="status" column="status"/>
<property name="updated" column="updt_timestamp"/>
</class>
<subclass name="Closeout"
extends="com.nlg.nlgv.paris.model.Restriction" discriminator-value="18">
<property name="travelStart" column="argument_1" type="date"/>
<property name="travelEnd" column="argument_2" type="date"/>
</subclass>
</hibernate-mapping>
The following HQL call (wrapped in Spring):
Code:
return getHibernateTemplate().find("from Closeout c where c.targetId=?", rateCodeId);
results in this SQL (columns omitted for clarity):
Code:
select *
from calendar_rule_vw closeout0_
where closeout0_.predicate_type_id=18
and ((closeout0_.rule_target_id=? ))
This works as I'd expect - the discriminator column is used to return only rows where predicate_type_id=18 and only instances of the Closeout subclass are returned. So far so good.
I also reference Closeout via one-to-many assocation from a RateCode using this mapping:
Code:
<hibernate-mapping package="com.nlg.nlgv.paris.model">
<class name="RateCode" table="p_rate_code">
<id name="id" column="rate_code_id">
<generator class="native"/>
</id>
<property name="code" column="external_rate_code"/>
<property name="private" column="is_private"/>
<property name="status" column="status"/>
<property name="description" column="rate_code_desc"/>
<many-to-one name="rateCodeBase" class="RateCodeBase" column="rate_code_base_id"/>
<many-to-one name="ratePlan" class="RatePlan" column="rate_plan_id"/>
<many-to-one name="rateDerivation" class="RateDerivation" column="rate_derivation_id"/>
<set name="closeouts" lazy="true">
<key column="rule_target_id"/>
<one-to-many class="com.nlg.nlgv.paris.model.Closeout"/>
</set>
<property name="created" column="create_timestamp"/>
<property name="updated" column="updt_timestamp"/>
<property name="createUserId" column="create_userid"/>
<property name="updateUserId" column="updt_userid"/>
</class>
</hibernate-mapping>
When I call RateCode.getCloseouts(), I would expect to get SQL similar to that above that uses the discriminator to select only Closeouts (again, those rows where predicate_type_id=18). Instead, I get the following:
Code:
select *
from calendar_rule_vw closeouts0_
where closeouts0_.rule_target_id=?
In this case, the discriminator column is not used and I can't figure out why. The success with HQL leads me to believe that the polymorphic mapping is correct. So perhaps there is something amiss with the mapping for RateCode? (Though it seems straightforward.) Any insight is much appreciated, thanks.