JavaWeb学习:Hibernate多对多关系

2021-03-18 03:27

阅读:518

标签:sys   属性   begin   tab   ack   mem   XML   ash   efault   

一、创建表

CREATE TABLE sys_user (
  user_id bigint IDENTITY(1,1) NOT NULL PRIMARY Key,
  user_code varchar(32) NULL,
  user_name varchar(64) NULL,
  user_password varchar(32) NULL ,
  user_state char(1) NULL 
)

CREATE TABLE sys_role (
  role_id bigint IDENTITY(1,1) NOT NULL PRIMARY Key,
  role_name varchar(32) NULL,
  role_memo varchar(128) DEFAULT NULL
)

CREATE TABLE sys_user_role (
  role_id bigint NOT NULL,
  user_id bigint NOT NULL,
  PRIMARY KEY (role_id,user_id),
  CONSTRAINT FK_user_role_role_id FOREIGN KEY (role_id) REFERENCES sys_role (role_id) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT FK_user_role_user_id FOREIGN KEY (user_id) REFERENCES sys_user (user_id) ON DELETE NO ACTION ON UPDATE NO ACTION
)

二、创建持久化类

①、实体类

public class User {
    private Long user_id;
    private String user_code;
    private String user_name;
    private String user_password;
    private String user_state;
    // 设置多对多关系:表示一个用户选择多个角色?
    // 放置的是角色的集合
    private SetRole> roles = new HashSetRole>();
}

public class Role {
    private Long role_id;
    private String role_name;
    private String role_memo;
    // 一个角色被多个用户选择:
    // 放置的是用户的集合
    private SetUser> users = new HashSetUser>();
}

②、映射文件

  User映射文件

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.hibernate.demo.User" table="sys_user">
        
        id name="user_id" column="user_id">
            generator class="native"/>
        id>
        
        property name="user_code" column="user_code"/>
        property name="user_name" column="user_name"/>
        property name="user_password" column="user_password"/>
        property name="user_state" column="user_state"/>
        
        
        set name="roles" table="sys_user_role" cascade="save-update,delete"  >
            
            key column="user_id"/>
            
            many-to-many class="com.hibernate.demo.Role" column="role_id"/>
        set>
    class>
hibernate-mapping>

  Role映射文件

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.hibernate.demo.Role" table="sys_role">
        
        id name="role_id" column="role_id">
            generator class="native"/>
        id>
        
        property name="role_name" column="role_name"/>
        property name="role_memo" column="role_memo"/>
        
        
        set name="users" table="sys_user_role" cascade="save-update,delete" inverse="true">
            
            key column="role_id"/>
            
            many-to-many class="com.hibernate.demo.User" column="user_id"/>
        set>
    class>
hibernate-mapping>

③、核心文件

mapping resource="com/hibernate/demo/User.hbm.xml"/>
mapping resource="com/hibernate/demo/Role.hbm.xml"/>

④、Java测试代码

@Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();
    
    User user1=new User();
    user1.setUser_name("zhangsan");
    User user2=new User();
    user2.setUser_name("lisi");
    
    Role role1=new Role();
    role1.setRole_name("admin");
    Role role2=new Role();
    role2.setRole_name("guests");
    Role role3=new Role();
    role3.setRole_name("Employee");
    
    // 双向关联
    user1.getRoles().add(role1);
    user1.getRoles().add(role3);
    
    user2.getRoles().add(role2);
    
    role1.getUsers().add(user1);
    role2.getUsers().add(user1);
    role3.getUsers().add(user2);
    
    session.save(user1);
    session.save(user2);
    session.save(role1);
    session.save(role2);
    session.save(role3);
    transaction.commit();
    }

三、多对多的其他操作

①、赋值用户角色

public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();
    
    User user=session.get(User.class, 1l);
    Role role=session.get(Role.class, 3l);
    
    user.getRoles().add(role);
    transaction.commit();
    }

②、改选用户角色

@Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();
    
    User user=session.get(User.class, 2l);
    Role role3=session.get(Role.class, 3l);
    Role role1=session.get(Role.class, 1l);
    user.getRoles().remove(role3);
    user.getRoles().add(role1);
    transaction.commit();
    }

③、删除用户角色

  
    @Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();
    
    User user=session.get(User.class, 1l);
    Role role2=session.get(Role.class, 2l);
    user.getRoles().remove(role2);
    transaction.commit();
    }

 

JavaWeb学习:Hibernate多对多关系

标签:sys   属性   begin   tab   ack   mem   XML   ash   efault   

原文地址:https://www.cnblogs.com/WarBlog/p/13954356.html


评论


亲,登录后才可以留言!