4
我有一個@ManyToMany
類型與@JoinTable
關聯的關係。Hibernate manytomany @JoinTable + @SecondaryTable重複條目
問題是關係中的實體有它自己的表,但有幾個屬性應該去關聯表。我覺得A_C
表是好的。
添加@SecondaryTable
重複。
@Entity
@Table(name = "A")
@SecondaryTable(name = "A_C", pkJoinColumns = {
@PrimaryKeyJoinColumn(columnDefinition = "A_ID", name = "A_ID")})
class A {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "A_B", joinColumns = {@JoinColumn(name = "A_ID")}, inverseJoinColumns = {@JoinColumn(name = "B_ID")})
private List<B> bs = new ArrayList<B>();
@Column(table = "A_B")
private int b1;
}
@Entity
@Table(name = "B")
@SecondaryTable(name = "A_B", pkJoinColumns = {
@PrimaryKeyJoinColumn(columnDefinition = "B_ID", name = "B_ID")})
class B {
@Column(table = "A_B")
private int a1;
@Column(table = "A_B")
private int a2;
@ManyToMany(mappedBy = "A_ID", fetch = FetchType.LAZY)
private List<A> as = new ArrayList<A>();
}
這種節省A
實體B
在某種程度上是重複的地方時:
A_ID B_ID a1 a2
1 0 1 1
1 1 0 0
凡應該是
A_ID B_ID a1 a2
1 1 1 1
隨着@Embeddable
將無法正常工作都不是。
ssedano。