Saturday, 14 September 2013

Hibernate @JoinTable

Hibernate @JoinTable

i am trying to map associations with @JionTable annotation, i have the
following situation.
public class A {
@Id
@GeneratedValue
private long id;
//getter setter...
}
public class D {
@Id
@GeneratedValue
private long id;
//getter setter..
}
public class B extends A {
@OneToMany( cascade = {CascadeType.ALL}, fetch = FetchType.LAZY,
orphanRemoval = true)
@JoinTable(
name="JOIN-TABLE",
joinColumns = @JoinColumn( name="B_ID"),
inverseJoinColumns = @JoinColumn( name="D_ID")
)
List<D> ds = new ArrayList<D>();
//getter setter...
}
public class C extends A {
@OneToMany( cascade = {CascadeType.ALL}, fetch = FetchType.LAZY,
orphanRemoval = true)
@JoinTable(
name="JOIN-TABLE",
joinColumns = @JoinColumn( name="C_ID"),
inverseJoinColumns = @JoinColumn( name="D_ID")
)
List<D> ds = new ArrayList<D>();
}
As you can see i am trying to save both associations (in class B and C) in
the same join table "JOIN-TABLE".
Wenn i call: session.save(b) session.save(c)
b is written to Database correctly but session.save(c) throws constraint
violation exception.
By default hibernate generates two join tables, but i want to save the
associations in one tablle, is this possible ??
Thanks in advance

No comments:

Post a Comment