I have a One-to-One mapping example working but not sure if its supposed to be working this way. Can anyone please let me know if the process I'm following is right ?
Code:
// The Persistance Code
Person person = makePerson(); // Faking
Address address = makeAddress(); // Faking
person.setAddress(address);
Integer i = (Integer)session.save(person);
address.setPerson(person);
address.setId(i);
i = (Integer)session.save(address);
// commit transaction
The classes roughly without the setters and getters
Code:
// Parent Class
class Person{
private Address address;
......
}
// Child
class Address{
private Person person;
......
}
The Mapping files. The domain objects have been fine-grained and hence the One-One relationship. Assuming a Person has only one Address, the mappings are :
Code:
// Person Mapping File
<hibernate-mapping>
<class name="Person" schema="TEST" table="PERSON">
<id column="PERSON_ID" name="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">PERSON_SEQ</param>
</generator>
</id>
<one-to-one name="address" class="Address" constrained="false" outer-join="false"/>
<property column="FIRST_NAME" length="30" name="firstName" not-null="true" type="string"/>
<property column="LAST_NAME" length="20" name="lastName" not-null="true" type="string"/>
<property column="SEX" length="7" name="sex" not-null="true" type="string"/>
</class>
</hibernate-mapping>
// Address Mapping
<hibernate-mapping>
<class name="Address" schema="TEST" table="ADDRESS">
<id column="PERSON_ID" name="id" unsaved-value="null">
<generator class="assigned" />
</id>
<property column="ADDRESS1" length="40" name="address1" not-null="true" type="string"/>
<property column="ADDRESS2" length="40" name="address2" type="string"/>
<property column="CITY" length="40" name="city" type="string"/>
<property column="STATE" length="30" name="state" type="string"/>
<property column="ZIP" length="22" name="zip" type="java.lang.Integer"/>
<one-to-one name="person" class="Person" constrained="true" outer-join="auto" />
</class>
</hibernate-mapping>