Hibernate 多对多关联一、 多对多关联多对多关联时,要实现向中间表插入数据.
操作步骤:
1. 代码建立两表之间关系 如(1)
2. 对一个表对象做增加操作注意:这个表对象必须是添加对象 如 (2)
{
2. 删除一条记录
Function function1 = securityService.getFunctionById("9");
Role role1 = securityService.getRoleById("6");
role1.getFunctions().remove(function1);
securityService.deleteFunctionByID("9");
例子:
SQL(MY SQL):create table tgroup ( group_id int not null auto_increment, name varchar(16) not null default '', primary key (group_id)) type=innodb;
create table trole ( role_id int not null auto_increment, name varchar(16) not null default '', primary key (role_id)) type=innodb;
create table tgroup_role ( group_id int not null, role_id int not null, primary key(group_id, role_id), index (group_id), foreign key (group_id) references tgroup(group_id), index (role_id), foreign key (role_id) references trole(role_id)) type=innodb;
Group 类:
package ivan.hibernate.many2many;
public class Group {
}
Group.hbm.xml:
Role 类:
package ivan.hibernate.many2many;
public class Role {
}
Role.hbm.xml:
hibernate.cfg.xml 同前面的例子差不多。
public class Test {
}
}