I need to map a parent child relationship, to a single table, both parent and child need to use the same primary key and the java code should be generated from the hbm.xml files. I am using hibernate 2.1 and hbm2java code generator
Parent.hbm.xml
Code:
<hibernate-mapping>
<class name="mapping.Parent" table="MAPPINGTEST">
<id name="id" type="java.lang.Integer" column="ID">
<generator class="sequence">
<param name="sequence">MAPPINGTEST_ID_SEQ</param>
</generator>
</id>
<property
name="firstName"
type="java.lang.String"
column="FNAME"
length="25"
/>
<property
name="lastName"
type="java.lang.String"
column="LNAME"
length="50"
/>
<property
name="age"
type="java.lang.Integer"
column="AGE"
length="11"
/>
</class>
</hibernate-mapping>
Child.hbm.xml
"This persistent object does not have its own primary key, it should be inherited from the parent"
Code:
<hibernate-mapping>
<class name="mapping.Child" table="MAPPINGTEST">
<meta attribute="extends">
mapping.Parent
</meta>
<id type="java.lang.Integer" column="ID">
<generator class="foreign">
<param name="table">MAPPINGTEST</param>
<param name="column">ID</param>
</generator>
</id>
<one-to-one
name="Parent"
class="mapping.Parent"
cascade="none"
constrained="true"
/>
<property
name="firstLineAdd"
type="java.lang.String"
column="address1"
length="30"
/>
<property
name="secondLineAddress"
type="java.lang.String"
column="address2"
length="30"
/>
</class>
</hibernate-mapping>
Is there a way I can get hibernate to use the parent objects <id> field as the child's primary key? What it seems to be doing at this point is creating a new id value instead of inheriting the parents field.
I also can use the <subclass> tag as I don't have a discriminator column available.
Many Thanks
Marc