2,遍历双方集合进行清空。
核心代码:
//删除id为6的客户Customer下的联系人LinkMan
Customer?customer1?=?session.get(Customer.class,?6L);
for?(LinkMan?linkMan?:?customer1.getLinkmans())?{
????linkMan.getCustomers().clear();
????session.delete(linkMan);
}
//删除该客户
customer1.getLinkmans().clear();
session.delete(customer1);
举个例子:
多对多双向关联
关系举例:老师<-->学生,老师需要知道自己教了哪些学生,学生也知道自己有哪些老师.
数据库:中间表
annotation:@manytomany
xml:
多对多单向配置只需要在一端进行配置就可以了,双向需要配置两端.
关系模型(teache多对多student)
teacher(id,name,students)多
set
student(id,name,teachers)多
set
annotation配置
在teacher这一端的students上配置
//如果手动指定生成的中间表的表名和字段名
@manytomany
@jointable(name="t_s",
joincolumns={@joincolumn(name="teacher_id")},
inversejoincolumns={@joincolumn(name="student_id")}
)
在student一端的teachers只需要配置
@manytomany(mappedby="students")
xml配置方式:两端配置一样,注意表名和生成的中间表的字段属性名要一致
teacher那一端配置
在student那一端配置
生成的表为
create table student (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table teacher (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
create table t_s (//生成的中间表
teacher_id integer not null,
student_id integer not null,
primary key (teacher_id, student_id)
)
t_s表的两个属性分别references其它表的主键.
t_s(teacher_id, student_id)为中间表,id策略为联合主键
达?矢抾哆拉?