I'm trying to make a one-to-one relation, i have two classes "Person" and "Phone". The problem is that when i try to insert a person who have a phone. Hibernate insert the phone right, and the person right.. but it doesn't save the relation attribute (the foreign key FK_idPhone in the person table).
This is just a test i'm doing to try to save a simple relation.
Hibernate version: 2.1.2
Database: postgre 7.4.2
Code:
public class Person {
private String name;
private Long id;
private Phone phone;
//getters and setters.....
}
Code:
public class Phone {
private String number;
private Long id;
//getters and setters.....
}
Code:
<hibernate-mapping>
<class name="uy.edu.ort.domain.services.ejb.Person" table="person">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="name"/>
<one-to-one name="phone" constrained="true" cascade="all" class="uy.edu.ort.domain.services.ejb.Phone"/>
</class>
<class name="uy.edu.ort.domain.services.ejb.Phone" table="phone">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="number"/>
</class>
</hibernate-mapping>
Code:
//This is the test
Person user = new Person();
Phone phone = new Phone();
user.setName("Guille");
phone.setNumber("123");
user.setPhone(phone);
session.save(user);