Hibernater学习笔记(五)

作者: Mr_欢先生 | 来源:发表于2017-12-13 17:31 被阅读65次

    上次学习一对多查询,今天学习多对多查询
    场景:用户和角色的关系,一个用户可以是多个角色,一个角色可以有多个用户

    • pojo类
      User.java
    public class User {
       private int uid;
        private String username;
        private String password;
        private String address;
        /**
         *一个用户多个角色
         */
        private Set<Role> roles = new HashSet<Role>();
    ....省略get@set方法....
    }
    

    Role.java

    public class Role {
        private Integer ro_id;
        private String ro_name;
        /**
         *角色描述
         */
        private String ro_memo;
    
        /**
         * 一个角色多个用户
         * @retu     */
        private Set<User> users = new HashSet<User>();
    ....省略get@set方法.....
    }
    
    • 配置文件
      User.hbm.xml
    <?xml version="1.0" encoding="utf-8" ?>
        <!DOCTYPE hibernate-mapping PUBLIC
                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.study.entity.User" table="h_user">
            <id name="uid" column="uid">
                <generator class="native"></generator>
            </id>
            <property name="username" column="username"></property>
            <property name="password" column="password"></property>
            <property name="address" column="address"></property>
            <!--name:角色set集合名称  table属性:第三张表名称-->
            <set name="roles" table="user_role">
                <!--第三张表中的的外键名称-->
                <key column="userid"></key>
                <!--角色全路径和角色第三张表的外键名称-->
                <many-to-many class="com.study.entity.Role" column="roleid"></many-to-many>
            </set>
        </class>
    </hibernate-mapping>
    

    Role.hbm.xml

    <?xml version="1.0" encoding="utf-8" ?>
        <!DOCTYPE hibernate-mapping PUBLIC
                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.study.entity.Role" table="h_role">
            <id name="ro_id" column="uid">
                <generator class="native"></generator>
            </id>
            <property name="ro_name" column="ro_name"></property>
            <property name="ro_memo" column="ro_memo"></property>
            <set name="users" table="user_role">
                <!--角色在第三章表中的外键-->
                <key column="roleid"></key>
                <many-to-many class="com.study.entity.User" column="userid"></many-to-many>
            </set>
        </class>
    </hibernate-mapping>
    
    创建成功

    1.多对多级联保存

    • 设置user用户配置文件
    <set name="roles" table="user_role" cascade="save-update">
                <!--第三张表中的的外键名称-->
                <key column="userid"></key>
                <!--角色全路径和角色第三张表的外键名称-->
                <many-to-many class="com.study.entity.Role" column="roleid"></many-to-many>
            </set>
    
    • 添加代码
    public void TestMany_to_Many_add(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                User user1 = new User();
                user1.setUsername("张用户");
                user1.setPassword("12345");
                user1.setAddress("西安");
    
                User user2 = new User();
                user2.setUsername("王用户");
                user2.setPassword("789456");
                user2.setAddress("广东");
    
                Role r1 = new Role();
                r1.setRo_name("总经理");
                r1.setRo_memo("总经理描述");
                Role r2 = new Role();
                r2.setRo_name("保安");
                r2.setRo_memo("保安描述");
                Role r3 = new Role();
                r3.setRo_name("司机");
                r3.setRo_memo("司机描述");
                //给用户1创建r1和r2的角色
                user1.getRoles().add(r1);
                user1.getRoles().add(r2);
                //给user2创建r2和r3的角色
                user2.getRoles().add(r2);
                user2.getRoles().add(r3);
                //保存用户
                session.save(user1);
                session.save(user2);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    多对多添加

    2.维护第三张表

    用户和惧色多对多关系,维护关系通过第三张表维护
    场景:为某个用户添加某个角色

     @Test
        public void manyToManyTableTest(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                User user =  session.get(User.class,3);
                Role role = session.get(Role.class,2);
                user.getRoles().add(role);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    

    删除用户和角色的关系

     @Test
        public void manyToManyDeleteTest(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                User user =  session.get(User.class,2);
                Role role = session.get(Role.class,3)       ;
                //让用户移除某个操作
                user.getRoles().remove(role);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    删除用户和角色关系
    上一篇:Hibernater学习笔记(四)
    当前文集 :Hibernate框架学习
    本笔记hibernate案例:github地址

    相关文章

      网友评论

      本文标题:Hibernater学习笔记(五)

      本文链接:https://www.haomeiwen.com/subject/abanixtx.html