-->
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.  [ 1 post ] 
Author Message
 Post subject: Bidirectional polymorphic associations
PostPosted: Thu Oct 29, 2009 3:52 pm 
Newbie

Joined: Thu Oct 29, 2009 2:33 pm
Posts: 1
I'm trying to get a bidirectional polymorphic association to work using table-per-subclass inheritence. I've found on the Advanced FAQ a question that's similar to what I'm trying to do, but the question's answer isn't clear to me. (FAQ maintainer: examples for solutions would be appreciated.)

I have a Person class and a Pet class, with subclasses of Cat and Dog. I want the Person class to be able to retrieve a set of Cats and a set of Dogs.

Here are my class mappings:

Code:
<class name="Pet" table="Pet">
   <id name="id" column="petId" type="long">
      <generator class="increment"/>
   </id>
   <version name="version"/>
   <many-to-one class="Person" column="personId" lazy="false" name="person" not-null="false" update="false"/>
   <joined-subclass name="Cat" table="Cat">
      <key column="petId"/>
      <property name="catAttribute" not-null="true" type="integer"/>
   </joined-subclass>
   <joined-subclass name="Dog" table="Dog">
      <key column="petId"/>
      <property name="dogAttribute" not-null="true" type="string"/>
   </joined-subclass>
</class>
<class name="Person" table="Person">
   <id name="id" column="personId" type="long">
      <generator class="increment"/>
   </id>
   <version name="version"/>
   <property name="name" not-null="true" type="string"/>
   <set name="catSet" cascade="all-delete-orphan" inverse="true">
      <key column="personId"> <!-- personId is in Pet, not Cat -->
      <one-to-many class="Cat"/>
   </set>
   <set name="dogSet" cascade="all-delete-orphan" inverse="true">
      <key column="personId"> <!-- personId is in Pet, not Dog -->
      <one-to-many class="Dog"/>
   </set>
</class>


The POJOs produced by hbm2java are unremarkable, but tables produced by hbm2ddl are:

Code:
mysql> describe Person;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| personId | bigint(20)   | NO   | PRI | NULL    |       |
| version  | int(11)      | NO   |     | NULL    |       |
| name     | varchar(255) | NO   |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe Pet;
+----------+------------+------+-----+---------+-------+
| Field    | Type       | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+-------+
| petId    | bigint(20) | NO   | PRI | NULL    |       |
| version  | int(11)    | NO   |     | NULL    |       |
| personId | bigint(20) | NO   | MUL | NULL    |       |
+----------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe Cat;
+--------------+------------+------+-----+---------+-------+
| Field        | Type       | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| petId        | bigint(20) | NO   | PRI | NULL    |       |
| catAttribute | int(11)    | NO   |     | NULL    |       |
| personId     | bigint(20) | YES  | MUL | NULL    |       |
+--------------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe Dog;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| petId        | bigint(20)   | NO   | PRI | NULL    |       |
| dogAttribute | varchar(255) | NO   |     | NULL    |       |
| personId     | bigint(20)   | YES  | MUL | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)


The personId columns in Cat and Dog are unwanted, and populated with nulls, and do not have corresponding attributes in the POJOs. How do I get this to work properly?

Thanks,

Mike


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

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.