1.单向多对多关联必须使用连接表,它和有链接表的单向一对多很相似
2.主要代码@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="T_JC_test1_tese2",joinColumns=@JoinColumn(name="test1ID"),inverseJoinColumns=@JoinColumn(name="test2ID"))
3.例子
<span style="font-family:Microsoft YaHei;">//表一,学生表
@Entity
@Table(name = "T_JC_test1")
@Where(clause="recordStatus='"+GlobalConstant.FLAG_Y+"'")
public class Test1 extends BusinessEntity{
private static final long serialVersionUID = 2535128385272676564L;
private String name;
private List<Test2> test2 = new ArrayList<Test2>();<span style="white-space:pre"> </span>//引用表二
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="T_JC_test1_tese2",joinColumns=@JoinColumn(name="test1ID"),inverseJoinColumns=@JoinColumn(name="test2ID"))
public List<Test2> getTest2() {
return test2;
}
public void setTest2(List<Test2> test2) {
this.test2 = test2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//表二,地址表
@Entity
@Table(name = "T_JC_test2")
@Where(clause="recordStatus='"+GlobalConstant.FLAG_Y+"'")
public class Test2 extends BusinessEntity{
private static final long serialVersionUID = -7653945367481001205L;
private String addressDetatil;
public String getAddressDetatil() {
return addressDetatil;
}
public void setAddressDetatil(String addressDetatil) {
this.addressDetatil = addressDetatil;
}
}</span>