I have the following scenario:
@Entity
class A {
@Id
@GeneratedValue
private long dbId;
@OneToMany ( cascade = CascadeType.ALL )
@JoinColumn( name = "A_ID", referencedColumnName = "DBID" )
Set<C> setOfCs;
}
@Entity
class B {
@Id
@GeneratedValue
private long dbId;
@OneToMany ( cascade = CascadeType.ALL )
@JoinColumn( name = "B_ID", referencedColumnName = "DBID" )
Set<C> setOfCs;
}
@Entity
class C {
@Id
@GeneratedValue
private long dbId;
}
Table C is created with two columns A_ID and B_ID as foreign keys to A.DBID and B.DBID respectively. This makes little sense in my case as each element of C is either linked from A or (xor) B but not both at the same time (both relationships are one-to-many not many-to-many).
Is there a way to have the same tables (which are fine) without the foreign key constraint on A_ID and B_ID? When I set A->C Oracle complains that C.B_ID is not set.
Thanks
Matteo
Why don’t you use @JoinTable? it will remove any foreign key on table C.
and by the way do not annotate items, do it on its getters instead.