Hibernate中的多对多映射

Posted 十三弦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (?, ?)

 

以上是关于Hibernate中的多对多映射的主要内容,如果未能解决你的问题,请参考以下文章

一口一口吃掉Hibernate——多对多关联映射

Hibernate学习笔记 --- 创建基于中间关联表的多对多映射关系

hibernate多对多关联映射

hibernate多对多映射中间表有多余字段问题该如何映射

Hibernate,关系映射的多对一单向关联多对一双向关联一对一主键关联一对一外键关联多对多关系关联

Hibernate-ORM:12.Hibernate中的多对多关联关系