hibernate基础12:关联映射之基于主键的双项多对多

2021-02-17 19:16

阅读:462

标签:map   creat   session   rop   hibernate   code   export   gets   null   

1、Java实体bean类

package com.project.pojo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Role implements Serializable{
    private int id;
    private String name ;
    private Set resources = new HashSet();
    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 getResources() {
        return resources;
    }
    public void setResources(Set resources) {
        this.resources = resources;
    }
    public void setResources(Resource resource) {
        this.getResources().add(resource);
    }
    

}
package com.project.pojo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Resource implements Serializable{
    private int id;
    private String name;
    private Set roles = new HashSet();
    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 getRoles() {
        return roles;
    }
    public void setRoles(Set roles) {
        this.roles = roles;
    }

}

2、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 package="com.project.pojo">
    class name="Role" table="t_role">
        id name="id">
            generator class="native" />
        id>
        property name="name" />
        
        set name="resources" table="t_role_resource">
            
            key column="roid" />
            
            many-to-many column="reid" class="Resource">many-to-many>
        set>
    class>

hibernate-mapping>
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 package="com.project.pojo">
    class name="Resource" table="t_resource">
        id name="id">
            generator class="native" />
        id>
        property name="name" />
        set name="roles" table="t_role_resource" inverse="true">
            key column="reid" />
            many-to-many column="roid" class="Role" />
        set>
    class>

hibernate-mapping>

3、测试

package com.project.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.project.pojo.Resource;
import com.project.pojo.Role;
import com.project.util.HibernateUtil;

public class HibernateTest {
    Session session = null;
    Transaction ts = null;
    
    @Before
    public void setUp(){
        session = HibernateUtil.getSession();
        ts = session.beginTransaction();
    }
    
    @After
    public void tearDown(){
        HibernateUtil.closeSession();
    }
    
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        //使得hibernate映射信息转换为数据库识别的dll语言
        SchemaExport se = new SchemaExport(cfg);
        //第一个参数:是否打印dll语句
        //第二个参数:是否将dll到数据库中执行
        se.create(true, true);
    }
    
    @Test
    public void testInit(){
        try {
            Role ro1 = new Role();
            ro1.setName("普通用户");
            Role ro2 = new Role();
            ro2.setName("VIP");
            
            Resource re1 = new Resource();
            re1.setName("查看");
            Resource re2 = new Resource();
            re2.setName("新增");
            Resource re3 = new Resource();
            re3.setName("修改");
            Resource re4 = new Resource();
            re4.setName("删除");
            session.save(re1);
            session.save(re2);
            session.save(re3);
            session.save(re4);
            
            //设置关系
            ro1.setResources(re1);
            ro1.setResources(re2);
            
            ro2.setResources(re1);
            ro2.setResources(re3);
            ro2.setResources(re4);
            session.save(ro1);
            session.save(ro2);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(ts!=null)ts.rollback();
        }
    }
    
    @Test
    public void testSelect(){
        Role ro = (Role) session.get(Role.class, 1);
        System.out.println(ro.getName());
        System.out.println("-----------");
        for(Resource r : ro.getResources()){
            System.out.println(r.getName());
        }
        System.out.println("=============");
        Resource re = (Resource) session.get(Resource.class, 1);
        System.out.println(re.getName());
        System.out.println("-------------");
        for(Role r : re.getRoles()){
            System.out.println(r.getName());
        }
    }
}

 

hibernate基础12:关联映射之基于主键的双项多对多

标签:map   creat   session   rop   hibernate   code   export   gets   null   

原文地址:https://www.cnblogs.com/chai-blogs/p/12951327.html


评论


亲,登录后才可以留言!