如何为以下案例创建注释:
Table A
id
name
Table B
id
name
Table C
id
name
Table AB
a_id
b_id
Table ABC
id
a_id
b_id
c_id
name
如何使用注释在hibernate上表示表ABC?
表AB不是用作表A和B之间的表连接的对象。
A和B的注释看起来像这样:
对于A:
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="AB",
joinColumns={@JoinColumn(name="a_id", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="b_id", referencedColumnName="id")})
private Set bs;
对于B:
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="AB",
joinColumns={@JoinColumn(name="b_id", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="a_id", referencedColumnName="id")})
private Set as;
先谢谢。
答案 0 :(得分:3)
我会做那样的事情:
A:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinTable(name = "AB", joinColumns = @JoinColumn(name = "A_ID",
referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "B_ID",
referencedColumnName = "ID"))
private List<B> bs;
B相同。
ABC
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_ID")
private A a;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_ID")
private B b;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "C_ID")
private C c;