zoukankan      html  css  js  c++  java
  • hibernate配置多对多ORM映射关系

    多对多关系,User和Role

    1.创建表

    CREATE TABLE `sys_role` (
      `role_id` int(11) NOT NULL AUTO_INCREMENT,
      `role_name` varchar(255) DEFAULT NULL,
      `role_memo` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`role_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
    
    
    CREATE TABLE `sys_user` (
      `user_id` int(11) NOT NULL AUTO_INCREMENT,
      `user_code` varchar(255) DEFAULT NULL,
      `user_name` varchar(255) DEFAULT NULL,
      `user_password` varchar(255) DEFAULT NULL,
      `user_state` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`user_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    
    CREATE TABLE `sys_user_role` (
      `user_id` int(11) NOT NULL,
      `role_id` int(11) NOT NULL,
      PRIMARY KEY (`user_id`,`role_id`),
      KEY `FK660C51789B2C831D` (`role_id`),
      KEY `FK660C5178405746FD` (`user_id`),
      CONSTRAINT `FK660C5178405746FD` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`user_id`),
      CONSTRAINT `FK660C51789B2C831D` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`role_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    2.创建实体

    import java.util.Set;
    import java.util.HashSet;
    
    public class User {
           private Integer user_id;
           private String user_code;
           private String user_name;
           private String user_password;
           private String user_state;
           private Set<Role> role =new HashSet<>();
           
        public Set<Role> getRole() {
            return role;
        }
        public void setRole(Set<Role> role) {
            this.role = role;
        }
        public Integer getUser_id() {
            return user_id;
        }
        public void setUser_id(Integer user_id) {
            this.user_id = user_id;
        }
        public String getUser_code() {
            return user_code;
        }
        public void setUser_code(String user_code) {
            this.user_code = user_code;
        }
        public String getUser_name() {
            return user_name;
        }
        public void setUser_name(String user_name) {
            this.user_name = user_name;
        }
        public String getUser_password() {
            return user_password;
        }
        public void setUser_password(String user_password) {
            this.user_password = user_password;
        }
        public String getUser_state() {
            return user_state;
        }
        public void setUser_state(String user_state) {
            this.user_state = user_state;
        }
           
           
    }
    import java.util.HashSet;
    import java.util.Set;
    
    public class Role {
          private Integer role_id;
          private String  role_name;
          private String  role_memo;
          private Set<User> user=new HashSet<>();
          
        public Set<User> getUser() {
            return user;
        }
        public void setUser(Set<User> user) {
            this.user = user;
        }
        public Integer getRole_id() {
            return role_id;
        }
        public void setRole_id(Integer role_id) {
            this.role_id = role_id;
        }
        public String getRole_name() {
            return role_name;
        }
        public void setRole_name(String role_name) {
            this.role_name = role_name;
        }
        public String getRole_memo() {
            return role_memo;
        }
        public void setRole_memo(String role_memo) {
            this.role_memo = role_memo;
        }
          
    }

    3.创建配置文件,注意外键的两次设置

    <?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.yqg.hibernate.entity">
           <class name="User" table="sys_user">
           <id name="user_id" column="user_id">
              <generator class="native"></generator>
           </id>
           <property name="user_code" column="user_code"></property>
            <property name="user_name" column="user_name"></property>
             <property name="user_password" column="user_password"></property>
              <property name="user_state" column="user_state"></property>
              <!-- 多对多关系 -->
              <set name="role" table="sys_user_role" cascade="save-update,delete">
                  <key column="user_id"></key>
                  <many-to-many class="Role" column="role_id"></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.yqg.hibernate.entity">
        <class name="Role" table="sys_role">
           <id name="role_id" column="role_id">
           <generator class="native"></generator>
           </id>
           <property name="role_name" column="role_name"></property> 
           <property name="role_memo" column="role_memo"></property>
           <set name="user" table="sys_user_role" inverse="true" cascade="save-update,delete">
              <key column="role_id"></key>
              <many-to-many class="User" column="user_id"></many-to-many>
           </set>
        </class>
        </hibernate-mapping>

    4.编写测试类

    测试级联保存
    /*
     * 多对多映射
     */
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.junit.Test;
    
    import com.yqg.hibernate.entity.Role;
    import com.yqg.hibernate.entity.User;
    import com.yqg.hibernate.utils.hibernateUtils;
    
    public class Demo1 {
      @Test
      //双向维护关系,注意被动的一方需要设置inverse为true,放弃维护外键
      public void Demo() {
          Session session = hibernateUtils.getCurrentSession();
          Transaction transaction = session.beginTransaction();
          User user=new User();
          user.setUser_name("ooo");
          User user1=new User();
          user1.setUser_name("ppp");
          Role role=new Role();
          role.setRole_name("uuu");
          Role role2=new Role();
          role2.setRole_name("rrr");
          Role role3=new Role();
          role3.setRole_name("fff");
          
          user.getRole().add(role);
          user.getRole().add(role2);
          user.getRole().add(role3);
          user1.getRole().add(role);
          user1.getRole().add(role2);
          user1.getRole().add(role3);
          
          role.getUser().add(user1);
          role.getUser().add(user);
          role2.getUser().add(user1);
          role2.getUser().add(user);
          role3.getUser().add(user1);
          role3.getUser().add(user);
          
    //      session.save(user1);
    //      session.save(user);
          session.save(role);
          session.save(role2);
          session.save(role3);
          transaction.commit();
      }
    }
    测试级联删除,注意删除时后两个用户不能有相同的角色重叠,否则会报外键错误。
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    /*级联删除
     * */
    import org.junit.Test;
    
    import com.yqg.hibernate.entity.Role;
    import com.yqg.hibernate.entity.User;
    import com.yqg.hibernate.utils.hibernateUtils;
    
    public class Demo2 {
       @Test
       public void demo() {
           Session session = hibernateUtils.getCurrentSession();
              Transaction transaction = session.beginTransaction();
    //          User user = (User) session.get(User.class, 1);
    //          session.delete(user);
             Role role = (Role) session.get(Role.class, 2);
              session.delete(role);
              transaction.commit();
          
       }
    }

    5.注意点

    不要忘了在核心配置文件配置资源文件。

  • 相关阅读:
    how to read openstack code: loading process
    how to read openstack code: request extension
    how to read openstack code: action extension
    b站刷赞 B站刷赞工具 bilibili评论刷赞 b站点赞
    利用邓西百度网盘消息群发工具对百度网盘的群组、好友进行管理,批量分享文件
    如何利用邓西百度网盘批量转存检测工具批量检测百度网盘失效分享链接
    如何使用邓西百度网盘批量重命名工具对百度网盘中的文件进行批量改名、删除等
    如何利用邓西百度网盘消息群发工具批量删除百度网盘单向好友
    如何利用工具自动通过百度网盘好友请求并发送消息或文件
    邓西百度网盘批量保存检测工具高级用法之分享链接以指定名称保存
  • 原文地址:https://www.cnblogs.com/yanqingguo/p/9752195.html
Copyright © 2011-2022 走看看