hibernate框架多对多-级联保存(十八)

来源:互联网 发布:全国软件开发考试报名 编辑:程序博客网 时间:2024/05/22 09:58

一、多对多级联保存

多对多级联保存,只要在上一篇的基础上对User.hbm.xml配置稍作修改

1.1 配置


多对多级联的时候,一方配置inverser,另一方就配置cascade

1.2 测试程序

@Testpublic void run2() {Session session = HibernateUtils.getCurrentSession();Transaction tx = session.beginTransaction();// 模拟多对多,双向的关联// 创建用户User u1 = new User();u1.setUsername("zhangsan");User u2 = new User();u2.setUsername("lisi");// 创建角色Role r1 = new Role();r1.setRname("manager");Role r2 = new Role();r2.setRname("actor");// 关联u1.getRoles().add(r1);u1.getRoles().add(r2);u2.getRoles().add(r1);session.save(u1);session.save(u2);tx.commit();}

1.3 运行结果

截图就不贴了(反正和上一篇都一样),看sql吧

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate:     insert     into        sys_user        (username, password)     values        (?, ?)Hibernate:     insert     into        sys_role        (rname)     values        (?)Hibernate:     insert     into        sys_role        (rname)     values        (?)Hibernate:     insert     into        sys_user        (username, password)     values        (?, ?)Hibernate:     insert     into        sys_user_role        (uid, rid)     values        (?, ?)Hibernate:     insert     into        sys_user_role        (uid, rid)     values        (?, ?)Hibernate:     insert     into        sys_user_role        (uid, rid)     values        (?, ?)

二、操作中间表-删除

想要操作中间表,我们只需要操作javabean中的集合

2.1 测试程序

/** * 现在:zhangsan用户,有2个角色,manager和actor  * 让zhangsan没有actor角色 */@Testpublic void run3() {Session session = HibernateUtils.getCurrentSession();Transaction tx = session.beginTransaction();// 查询zhangsan用户User u1 = session.get(User.class, 1L);// 查询角色Role r2 = session.get(Role.class, 2L);u1.getRoles().remove(r2);tx.commit();}

2.2 运行结果


看sql:

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate:     select        user0_.uid as uid1_1_0_,        user0_.username as username2_1_0_,        user0_.password as password3_1_0_     from        sys_user user0_     where        user0_.uid=?Hibernate:     select        role0_.rid as rid1_0_0_,        role0_.rname as rname2_0_0_     from        sys_role role0_     where        role0_.rid=?Hibernate:     select        roles0_.uid as uid1_2_0_,        roles0_.rid as rid2_2_0_,        role1_.rid as rid1_0_1_,        role1_.rname as rname2_0_1_     from        sys_user_role roles0_     inner join        sys_role role1_             on roles0_.rid=role1_.rid     where        roles0_.uid=?Hibernate:     delete     from        sys_user_role     where        uid=?         and rid=?

三、操作中间表-添加

3.1 测试程序

/** * 现在:zhangsan用户,有1个角色,manager  * 让zhangsan添加actor角色 */@Testpublic void run4() {Session session = HibernateUtils.getCurrentSession();Transaction tx = session.beginTransaction();// 查询zhangsan用户User u1 = session.get(User.class, 1L);// 查询角色Role r2 = session.get(Role.class, 2L);u1.getRoles().add(r2);tx.commit();}

3.2 运行结果


看sql:

log4j:WARN No appenders could be found for logger (org.jboss.logging).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate:     select        user0_.uid as uid1_1_0_,        user0_.username as username2_1_0_,        user0_.password as password3_1_0_     from        sys_user user0_     where        user0_.uid=?Hibernate:     select        role0_.rid as rid1_0_0_,        role0_.rname as rname2_0_0_     from        sys_role role0_     where        role0_.rid=?Hibernate:     select        roles0_.uid as uid1_2_0_,        roles0_.rid as rid2_2_0_,        role1_.rid as rid1_0_1_,        role1_.rname as rname2_0_1_     from        sys_user_role roles0_     inner join        sys_role role1_             on roles0_.rid=role1_.rid     where        roles0_.uid=?Hibernate:     insert     into        sys_user_role        (uid, rid)     values        (?, ?)
源码下载

原创粉丝点击