I am using Hibernate 3.1 and Oracle 9i. I am trying to set up a one-to-one relationship between the PERSON table and the EMPLOYEE table, where EMPLOYEE should be assigned the same primary key as PERSON.
I have set up the mapping files the way the Hibernate documentation showed:
Mapping documents:
Code:
<class name="org.Person" table="PERSON">
<id name="personId" column="PERSON_ID">
<generator class="increment">
</generator>
</id>
<one-to-one name="employee" class="org.Employee" cascade="save-update">
</one-to-one>
</class>
Code:
<class name="org.Employee" table="EMPLOYEE">
<id name="id" column="EMPLOYEE_ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" class="org.Person" constrained="true">
</one-to-one>
</class>
Code to save Person:Code:
1.) Person person = new Person();
2.) Employee employee = new Employee();
3.) person.setEmployee(employee);
4.) employee.setPerson(employee);
5.) dao.savePerson(person);
When I run this code, I get an exception at line 5 saying the following:
Code:
nested exception is org.hibernate.HibernateException: Unable to resolve property: person
If I save the person object first between lines 1 and 2, I do not get the same exception, but the employee object still does not get saved when the person is saved again at line 5.
I am wondering if there is anything special I need to do in the getters and setters of the Person and Employee classes. Right now all I am doing is passing in the object.
setPerson method of Employee class:
Code:
public void setPerson(Person person) {
this.person = person;
}
setEmployee method of Person class:
Code:
public void setEmployee(Employee employee) {
this.employee = employee;
}
Can anyone tell me what I need to do?