• java之hibernate之多对多双向关联映射


    1.比如在权限管理中,角色和权限之间的关系就是多对多的关系,表结构为:

    2.类结构

    Role.java

    public class Role implements Serializable{
        private int id;
        private String name;
        private Set<Permission> permissions=new HashSet<Permission>();
        public Role() {
        }
        public Role(String name) {
            super();
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Set<Permission> getPermissions() {
            return permissions;
        }
        public void setPermissions(Set<Permission> permissions) {
            this.permissions = permissions;
        }
    }

    Permisssion.java

    public class Permission implements Serializable{
        private int id;
        private String name;
        private Set<Role> roles;
        public Permission() {
            // TODO Auto-generated constructor stub
        }
        
        public Permission(String name) {
            super();
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public Set<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
    }

    3.映射文件信息

    Role.hbm.xml

    <hibernate-mapping package="cn.sxt.pojo">
        <class name="Role" table="t_role">
            <id name="id">
                <generator class="native">
                </generator>
            </id>
            <property name="name"/>
            <set name="permissions" table="t_role_permission">
                <!-- 当前类在连接表中的外键 -->
                <key column="rid"></key>
                <many-to-many column="pid" class="Permission"></many-to-many>
            </set>
        </class>
    </hibernate-mapping>

    Permission.hbm.xml

    <hibernate-mapping package="cn.sxt.pojo">
        <class name="Permission" table="t_permission">
            <id name="id">
                <generator class="native">
                </generator>
            </id>
            <property name="name"/>
            <set name="roles" table="t_role_permission" inverse="true">
                <!-- 当前类在连接表中的外键 -->
                <key column="pid"></key>
                <many-to-many column="rid" class="Role"></many-to-many>
            </set>
        </class>
    </hibernate-mapping>

    4.测试

    public class HibernateTest {
        /**
         * 生成数据库表的工具方法
         * */
        @Test
        public void testCreateDB(){
            Configuration cfg = new Configuration().configure();
            SchemaExport se = new SchemaExport(cfg);
            //第一个参数  是否打印sql脚本
            //第二个参数 是否将脚本导出到数据库中执行
            se.create(true, true);
        }
        /**
         * 初始化表数据
         */
        @Test
        public void testInit(){
            Session session = null;
            Transaction tx = null;
            try {
                session = HibernateUtil.getSession();
                tx = session.beginTransaction();
                Permission p1 = new Permission("增加用户");
                Permission p2 = new Permission("删除用户");
                Permission p3 = new Permission("查询用户");
                Permission p4 = new Permission("修改用户");
                
                Role r1 = new Role("管理员");
                r1.getPermissions().add(p1);
                r1.getPermissions().add(p2);
                r1.getPermissions().add(p3);
                r1.getPermissions().add(p4);
                Role r2 = new Role("vip");
                r2.getPermissions().add(p3);
                r2.getPermissions().add(p4);
                
                session.save(p1);
                session.save(p2);
                session.save(p3);
                session.save(p4);
                session.save(r1);
                session.save(r2);
                tx.commit();
                
            } catch (Exception e) {
                if(tx!=null)
                    tx.rollback();
            }finally {
                HibernateUtil.close();
            }
        }
        /**
         */
        @Test
        public void testGetData(){
            Session session = HibernateUtil.getSession();
            Role role = (Role)session.get(Role.class, 1);
            System.out.println(role.getId()+"---"+role.getName());
            System.out.println("-------------");
            for(Permission p:role.getPermissions()){
                System.out.println(p.getId()+"---"+p.getName());
            }
            System.out.println("==========================");
            Permission p = (Permission)session.get(Permission.class, 3);
            System.out.println(p.getId()+"----"+p.getName());
            for(Role r:p.getRoles()){
                System.out.println(r.getId()+"---"+r.getName());
            }
            HibernateUtil.close();
        }
    }
  • 相关阅读:
    servlet简介
    synchronized锁的升级过程
    volatile的作用及原理
    redis数据类型
    mysql的主从复制
    redis的缓存穿透,缓存击穿,缓存雪崩
    网络从io到多路复用
    mysql索引
    mysql的执行计划
    mysql 常用函数
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/11204764.html
Copyright © 2020-2023  润新知