Hibernate version:2.1.6
Hibernate supports primary key associations that may be unidirectional (at either end). The Unidirectional mapping files below show this.
However, is it still possible to use the <generator class="foreign"/> to guarantee that when the primary keys are generated they are identical?
Otherwise we have to handle this in the Java object model rather than transparently inside Hibernate.
So far I have seen that if we want to change "assigned" to "foreign" below, the mapping rules force us to add a property and association to the child for the parent, making it bidirectional.
Unidirectional Mapping documents:
Code:
<hibernate-mapping package="test">
<class name="Person" table="PARENT">
<id name="parentId" type="long" column="PARENT_ID">
<generator class="sequence">
<param name="sequence">PARENTSEQ</param>
</generator>
</id>
<property name="parentName" type="string" column="PARENT_NAME"/>
<one-to-one name="child" class="Child" cascade="all"/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping package="test">
<class name="Child" table="CHILD">
<id name="childId" type="long" column="ID">
<generator class="assigned"/>
</id>
<property name="childName" type="string" column="CHILD_NAME"/>
</class>
</hibernate-mapping>
Bidirectional Mapping for child:Code:
<hibernate-mapping package="test">
<class name="Child" table="CHILD">
<id name="childId" type="long" column="ID">
<generator class="foreign">
<param name="property">parent</param>
</generator>
</id>
<property name="childName" type="string" column="CHILD_NAME"/>
<one-to-one name="parent" class="Parent" constrained="true"/>
</class>
</hibernate-mapping>
Thanks in advance
Ken