In the book it says when you declare a @OnetoOne mapping Hibernate will enforce the multiplicity with the unique constraint.
I wrote a sample program
Code:
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long studentId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="Student_Address_FK")
private Address studentAddress;
...
}
Code:
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue
@Column(name = "ADDRESS_ID")
private long addressId;
@OneToOne(mappedBy="studentAddress")
private Student student;
Code:
public static void main(String args[]) {
EntityTransaction tx = em.getTransaction();
tx.begin();
Address address1 = new Address();
Student student1 = new Student("Eswar", address1);
Student student2 = new Student("Joe", address1);
em.persist(student2);
em.persist(student1);
tx.commit();
em.close();
}
Then I check the database and in the student table both of the rows have the same Address_FK. Correct me if I am wrong, but in this case hibernate does not enforce uniqueness (1-to-1).