Hibernate多对多关联

Hibernate 多对多关联一、 多对多关联多对多关联时,要实现向中间表插入数据.
操作步骤:
1. 代码建立两表之间关系 如(1)
2. 对一个表对象做增加操作注意:这个表对象必须是添加对象 如 (2)
{
  Role role1 = new Role();
  role1.setName("spring");
  Role role2 = new Role();
  role2.setName("java");
  Function function1 = new Function();
  function1.setFunctionCode("addrolefunction.jspa");
  securityService.addFunction(function1);
  (1) role1.getFunctions().add(function1);
  (1) role2.getFunctions().add(function1);
  (2) securityService.addRole(role1);
  (2) securityService.addRole(role2);}
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 {
  private int id;
  private String name;
  private Set roles = new HashSet(); ...
}
 
Group.hbm.xml:
Role 类:
package ivan.hibernate.many2many;
public class Role {
  private int id;
  private String name;
  private Set groups = new HashSet(); ...
}
Role.hbm.xml:
hibernate.cfg.xml 同前面的例子差不多。
public class Test {
  public static void main(String[] args) throws HibernateException {
  Role role1 = new Role();
  role1.setName("Role1");
  Role role2 = new Role();
  role2.setName("Role2");
  Role role3 = new Role();
  role3.setName("Role3");
  Group group1 = new Group();
  group1.setName("group1");
  Group group2 = new Group();
  group2.setName("group2");
  Group group3 = new Group();
  group3.setName("group3");
  group1.getRoles().add(role1);
  group1.getRoles().add(role2);
  group2.getRoles().add(role2);
  group2.getRoles().add(role3);
  group3.getRoles().add(role1);
  group3.getRoles().add(role3);
  role1.getGroups().add(group1);
  role1.getGroups().add(group3);
  role2.getGroups().add(group1);
  role2.getGroups().add(group2);
  role3.getGroups().add(group2);
  role3.getGroups().add(group3);
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  Transaction tx= session.beginTransaction();
  session.save(role1);
  session.save(role2);
  session.save(role3);
  session.save(group1);
  session.save(group2);
  session.save(group3);
  tx.commit();
  session.close();
  sessionFactory.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值