• 【Hibernate】---【注解】多对多


    一、核心配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
            <property name="hibernate.connection.username">root</property>
            
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="current_session_context_class">thread</property>
            <!-- 
            <mapping resource="com/chinasofti/entity/User.hbm.xml" />
            <mapping resource="com/chinasofti/entity/Role.hbm.xml" />
            -->
            <mapping class="com.chinasofti.entity.User"/>
            <mapping class="com.chinasofti.entity.Role"/>
        </session-factory>
    </hibernate-configuration>

    二、实体类

      User

    package com.chinasofti.entity;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name="T_USER3")
    public class User {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer user_id;
        private String user_name;
        private String user_password;
        
        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name="user_role3",joinColumns=@JoinColumn(name="user_id"),inverseJoinColumns=@JoinColumn(name="role_id"))
        private Set<Role> roles = new HashSet<Role>(); 
    
        public Set<Role> getRoles() {
            return roles;
        }
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
        public Integer getUser_id() {
            return user_id;
        }
        public void setUser_id(Integer user_id) {
            this.user_id = user_id;
        }
        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;
        }
        @Override
        public String toString() {
            return "User [user_id=" + user_id + ", user_name=" + user_name
                    + ", user_password=" + user_password + "]";
        }
    }

      Role

    package com.chinasofti.entity;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="T_ROLE3")
    public class Role {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer role_id;
        private String role_name;
        private String role_memo;
        
        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name="user_role3",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="user_id"))
        private Set<User> user = new HashSet<User>(); 
        
        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;
        }
        @Override
        public String toString() {
            return "Role [role_id=" + role_id + ", role_name=" + role_name
                    + ", role_memo=" + role_memo + "]";
        }
    }

    三、封装类

    package com.chinasofti.entity;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
        static Configuration CONFIGURATION = null;
        static SessionFactory SESSION_FACTORY = null;
        static Session SESSION = null;
        static{
            CONFIGURATION = new Configuration();
            CONFIGURATION.configure();
            SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
            SESSION    = SESSION_FACTORY.openSession();
        }
        public static SessionFactory getSessionFactory(){
            return SESSION_FACTORY;
        }
        public static Session openSession(){
            return SESSION;
        }
    }

    四、测试类

    package com.chinasofti.entity;
    
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.junit.Test;
    
    
    public class TestAnnotation {
        
        @Test
        public void removeRoleForUser1(){
            Session session = HibernateUtil.openSession();
            session.close();
        }
        
    
        /**
         * 添加用户以、角色
         */
        @Test
        public void testManytoManyInsert(){
            Session session = HibernateUtil.openSession();
            Transaction transaction = session.beginTransaction();
            //创建第一个用户
            User user1 =new User();
            user1.setUser_name("张三");
            user1.setUser_password("zhagsan");
            //创建第二个用户
            User user2 = new User();
            user2.setUser_name("李四");
            user2.setUser_password("lisi");
            //创建第三个用户
            User user3 =new User();
            user3.setUser_name("王五");
            user3.setUser_password("wangwu");
    
            //创建第一个角色
            Role role1 = new Role();
            role1.setRole_name("校长");
            role1.setRole_memo("校长");
            
            //创建第二个角色
            Role role2 = new Role();
            role2.setRole_name("主任");
            role2.setRole_memo("主任");
            
            //创建第三个角色
            Role role3 = new Role();
            role3.setRole_name("门房");
            role3.setRole_memo("门房");
            
            
            //创建第四个角色
            Role role4 = new Role();
            role4.setRole_name("体育老师");
            role4.setRole_memo("体育老师");
            
            
            //把角色放到用户里
            user1.getRoles().add(role1);
            user1.getRoles().add(role2);
            
            user2.getRoles().add(role2);
            user2.getRoles().add(role4);
            
            user3.getRoles().add(role3);
            user3.getRoles().add(role4);
            
            //保存
            session.save(user1);
            session.save(user2);
            session.save(user3);
            
            transaction.commit();
            
            session.close();
        }
    }

    补充:关系/对象映射 多对多关系(@ManyToMany 注释)【重新认识】

  • 相关阅读:
    how to write a paper
    attention mechanism思考
    OSX-KVM 安装备忘指南: 在KVM虚拟机中运行macOSX Big Sur / Catalina
    Unity CacheServer6.x版本 安装配置说明
    Linux常用命令2
    Ubuntu 18.04 + apache2.4 配置https证书(SSL)笔记
    Linux常用命令随笔
    软件测试-8 集成测试
    软件测试-7 在实际测试时的一些想法
    软件测试-6 白盒测试
  • 原文地址:https://www.cnblogs.com/angelye/p/7464903.html
Copyright © 2020-2023  润新知