• Hibernate中的多对多映射


    1.需求

    项目与开发员工

    一个项目,有多个开发人员

    一个开发人员,参与多个项目 【多对多】

    2.实体bean设计

      Project:

    public class Project {
        private int prj_id;
        private String prj_name;
        private Set<Developer> devs = new HashSet<Developer>();
        set...
        get...
    }

      Developer:

    public class Developer {
        private int d_id;
        private String d_name;
        private Set<Project> projects = new HashSet<Project>();
        set...
        get...
    }

    3.配置映射文件

      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="com.juaner.hibernate.project">
        <class name="Project" table="t_project">
            <!--主键-->
            <id name="prj_id" >
                <generator class="native"/>
            </id>
            <property name="prj_name"  type="string"/>
            <set cascade="save-update" inverse="true" name="devs" table="t_relation">
                <key column="prjid"></key>
                <many-to-many column="devid" class="Developer"/>
            </set>
        </class>
    
    </hibernate-mapping>

      Developer.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.juaner.hibernate.project">
        <class name="Developer" table="t_developer">
            <!--主键-->
            <id name="d_id">
                <generator class="native"/>
            </id>
            <property name="d_name" type="string"/>
            <set name="projects" table="t_relation" >
                <!--外键字段-->
                <key column="devid"></key>
                <many-to-many column="prjid" class="Project"/>
            </set>
        </class>
    
    </hibernate-mapping>

    4.保存数据

        @Test
        public void test1(){
            Session session = sf.openSession();
            session.beginTransaction();
    
            Project project_wuliu = new Project();
            project_wuliu.setPrj_name("物流系统");
            Project project_oa = new Project();
            project_oa.setPrj_name("OA系统");
    
            Developer developer_1 = new Developer();
            developer_1.setD_name("曹集");
            Developer developer_2 = new Developer();
            developer_2.setD_name("王睿");
            Developer developer_3 = new Developer();
            developer_3.setD_name("老张");
    
            project_wuliu.getDevs().add(developer_1);
            project_wuliu.getDevs().add(developer_2);
    
            project_oa.getDevs().add(developer_3);
            project_oa.getDevs().add(developer_2);
    
            session.save(project_wuliu);
            session.save(project_oa);
            session.save(developer_1);
            session.save(developer_2);
            session.save(developer_3);
    
    
            session.getTransaction().commit();
            session.clear();
    
        }

      此时,会生成9条sql语句:

            Hibernate: insert into t_project (prj_name) values (?)
            Hibernate: insert into t_project (prj_name) values (?)
            Hibernate: insert into t_developer (d_name) values (?)
            Hibernate: insert into t_developer (d_name) values (?)
            Hibernate: insert into t_developer (d_name) values (?)
            Hibernate: insert into t_relation (prjid, devid) values (?, ?)
            Hibernate: insert into t_relation (prjid, devid) values (?, ?)
            Hibernate: insert into t_relation (prjid, devid) values (?, ?)
            Hibernate: insert into t_relation (prjid, devid) values (?, ?)
  • 相关阅读:
    POJ 2533 Longest Ordered Subsequence(裸LIS)
    HDU 1159 Common Subsequence(裸LCS)
    HDU 1160(两个值的LIS,需dfs输出路径)
    HDU 1260 Tickets (普通dp)
    HDU 2859 Phalanx(对称矩阵 经典dp样例)
    2018年暑假ACM个人训练题7 题解报告
    HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)
    HDU 1071 The area(求三个点确定的抛物线的面积,其中一个点是顶点)
    HDU 1077 Catching Fish(用单位圆尽可能围住多的点)
    HDU 1099 Lottery (求数学期望)
  • 原文地址:https://www.cnblogs.com/juaner767/p/5564825.html
Copyright © 2020-2023  润新知