• 双向多对多


    1.在双方实体类里添加对方的集合

    Emp实体类:

    public class Emp {
        private Integer nid;
        private String empname;
        Set<Project> pros=new HashSet<Project>();
        
        
        public Emp() {
            super();
        }
        public Emp(Integer nid, String empname) {
            super();
            this.nid = nid;
            this.empname = empname;
        }
        public Integer getNid() {
            return nid;
        }
        public void setNid(Integer nid) {
            this.nid = nid;
        }
        public String getEmpname() {
            return empname;
        }
        public void setEmpname(String empname) {
            this.empname = empname;
        }
        public Set<Project> getPros() {
            return pros;
        }
        public void setPros(Set<Project> pros) {
            this.pros = pros;
        }
    }

    Project实体类:

    public class Project {
        private int pid;
        private String pname;
        private Set<Emp> emps=new HashSet<Emp>();
        
        
        public Project() {
            super();
        }
        public Project(int pid, String pname) {
            super();
            this.pid = pid;
            this.pname = pname;
        }
        public Set<Emp> getEmps() {
            return emps;
        }
        public void setEmps(Set<Emp> emps) {
            this.emps = emps;
        }
        public int getPid() {
            return pid;
        }
        public void setPid(int pid) {
            this.pid = pid;
        }
        public String getPname() {
            return pname;
        }
        public void setPname(String pname) {
            this.pname = pname;
        }
        
    }

    2.双向的多对多需要在双方的小配置里添加<set></set>元素  *需要加table属性*:

    Emp.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="cn.a.happy">
         <class name="Emp" table="EMP">
            <id name="nid" column="nid">
            <!-- native   数据库方言的可移植性 -->
               <generator class="native"></generator>
            </id>
            <property name="empname" type="string" column="empname"/>
            
            <!-- 多对多 -->
            <set name="pros" table="ProEmp" >
                <key column="nid"/>    <!-- 在哪个配置,就写配置对应的主键 -->
                <many-to-many class="Project" column="pid"/>    <!-- 对应多的一方成员类型 -->
            </set>
        </class>
    </hibernate-mapping>

    Project.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="cn.a.happy">
         <class name="Project" table="PROJECT">
            <id name="pid" column="pid">
               <generator class="native"></generator>
            </id>
            <property name="pname" type="string" column="pname"/>
             <!-- 多对多 
                   需要在<set>元素里 加一个table属性
                   table="生成第三张表的名字"
             -->
            <set name="emps" table="ProEmp" >
                <key column="pid"/>    <!-- 在哪个配置,就写配置对应的主键 -->
                <many-to-many class="Emp" column="nid"/>    <!-- 对应多的一方成员类型 -->
            </set>
            
        </class>
    </hibernate-mapping>

    进行测试:

    /**
             * 多对多双向关联测试
             * 添加操作
             */
            @Test
            public void muchVsMuchTest(){
                Emp emp1=new Emp(1,"李睿智");
                Emp emp2=new Emp(2,"小明");
                
                
                Project project1=new Project(1,"一号项目");
                Project project2=new Project(2,"二号项目");
                
                project1.getEmps().add(emp1);
                project1.getEmps().add(emp2);
                
                project2.getEmps().add(emp1);
                
                session.save(project1);
                session.save(project2);
            }
  • 相关阅读:
    中美贸易战再次开启,世界两极化进程正在加快形成!..... Copyright: 1688澳洲新闻网 Read more at: https://www.1688.com.au/world/international/2018/06/17/369368/
    缠师的博客中关于舒伯特中的回帖,细思极恐
    南怀瑾,脱了国学大师的外衣,只剩下江湖和名利
    你可能修了一个假的“不净观”
    陈大惠老师:什么叫道德?
    C#-正则,常用几种数据解析-端午快乐
    html5+go+websocket简单实例代码
    Task三个列子的分享
    golang-web框架revel一个表单提交的总结
    百度流行音乐-资源数据整合
  • 原文地址:https://www.cnblogs.com/qingzhi/p/5838512.html
Copyright © 2020-2023  润新知