tags:

views:

74

answers:

1

Hi there,

Is it possible to make a foreign key unique within a table? Suppose I have entities A and B.

A:

@Entity
class A extends Serializable {
@Id
private long id;

@OneToOne
private B b;
}

B:

@Entity
class B extends Serializable {
@Id
private long id;
}

I want to make it so that an A can have a B, except there can be no other A's with the same B. Ex: a1 has b1, and a2 has b2... in this case, a3 cannot have b1 or b2 since the B's must be unique.

Is there a way to accomplish this? I'd like to be able to put the @Column( unique = true ) annotation above the @OneToOne, but this doesn't seem to be possible.

+1  A: 

For sure you can add:

@OneToOne
@JoinColumn(name="COLUMN_NAME", unique=true)

Tomasz
For some reason when I do this, no table "A" gets created. I just get table B being generated...weird.
pushmatrix