Hibernate version:
Hibernate Core 3.2.5.GA
Hibernate Annotations 3.3.0.GA
Hibernate Entity Manager 3.3.1.GA
Name and version of the database you are using:
Microsoft SQL Server 2000/2005
We are trying to apply bidirectional one-to-one mapping with join table betwen two classes, but have problems with the generated SQL.
Consider simple example:
Code:
@Entity
public class Person {
@Id
private long id;
@OneToOne(optional = false)
@JoinTable(
name = "Person_Address",
joinColumns = @JoinColumn(name = "person_id"),
inverseJoinColumn = @JoinColumn(name = "address_id")
)
private Address address;
}
@Entity
public class Address {
@Id
private long id;
@OneToOne(mappedBy = "address")
private Person person;
}
Generated (partial) SQL for the join table looks like following:
Code:
create table Person_Address (
person_id numeric(19, 0) not null,
address_id numeric(19, 0) not null,
primary key (address_id),
unique(address_id)
)
It looks a little strange to have both primary key and unique constraint defined on the same colum, right? We think that correct would be to have primary key defined on one column and unique constraint defined on second column.
Our question is: What is the correct way of mapping to achieve desired behavior - primary key on one column and unique constraint on second?