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