-->
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.  [ 3 posts ] 
Author Message
 Post subject: joined-subclass, polymorphism and load() and find()
PostPosted: Fri Oct 17, 2003 2:49 pm 
Newbie

Joined: Wed Oct 15, 2003 11:16 am
Posts: 6
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]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 4:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
2) Why
Code:
from Product node
instead of
Code:
from CatalogNode node ...
does not work ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 4:20 pm 
Newbie

Joined: Wed Oct 15, 2003 11:16 am
Posts: 6
epbernard wrote:
2) Why
Code:
from Product node
instead of
Code:
from CatalogNode node ...
does not work ?


That does work, thanks. :-)

If I use
Code:
from Product node
I get only Product instances.

If I use
Code:
from CatalogItem node
I get CatalogItem and Product instances.

Semantically, this makes sense from a taxonomy p.o.v.; a Product is-a CatalogItem in the instanceof sense.

I was considering that the
Code:
node.class = Foo
usage might be more of an equality test on the class.

Code:
node.class = CatalogItem
would get me only CatalogItem instances, not subclasses as well (instanceof CatalogItem).


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

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.