I have a classic parent-child model in a single table based on a parent_id foreign key.
Some nodes may have additional attributes based on a joined-subclass mapping.
The base class is CatalogNode, and the joined-subclass is Product. The Product attributes are in a separate table, with the same primary key as the CatalogNode.
When I use getChildren(), the collection returned is indeed has a mixture of instances of the base class and subclass as defined by the joined-subclass. (great stuff!)
My questions are regarding the use of load() and find()
1) is it possible to use load() to get a known instance, still having the joined-subclass maping influence the actual class instantiated?
eg:
Code:
CatalogNode node (CatalogNode) sess.load(CatalogNode.class, id);
If the id refers to a row where the joined-subclass join succeeds (ie: it's a Product subclass), should I expect to get a Product instance?
What I get is a CatalogItem instance, which indeed gives me a ClassCastException at runtime if I try to cast to Product.
The same instance, when returned in the collection via it's parent's getChildren() method is a Product.
2) Can I use find() with joined-subclass to get only a specific class?
I see that this is indeed possible using a subclass with a discriminator value (very cool!), but is it possible when the 'discriminator' is the success/failure of a join of the joined-subclass?
Eg:
Code:
find("from CatalogNode node where node.class = Product")
If I use find() with no 'where' clause, I get all the nodes, and indeed the instances are of the appropriate types per the joined-subclass.
If I attempt to qualify with the node.class attribute (admittedly, a Hail Mary on my part!) I get only the instances of the base class.
Hibernate 2.1.b3, MySQL 4.01.4
Here's an abbreviated mapping:
Code:
<class name="CatalogNode" table="catalog_node">
<key column="id"/>
<property name="name"/>
<many-to-one
name="parent"
class="CatalogNode"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="parent_id"
/>
<set
name="children"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
order-by="name"
>
<key
column="parent_id"
/>
<one-to-many
class="CatalogNode"
/>
</set>
<joined-subclass
name="Product"
table="product"
dynamic-update="false"
dynamic-insert="false"
proxy="Product"
>
<key
column="id"
/>
<!-- additional properties for subclass ... -->
</joined-subclass>
</class>
[/code]