美文网首页
Hibernate初探之多对多映射

Hibernate初探之多对多映射

作者: 最最最最醉人 | 来源:发表于2018-01-16 11:21 被阅读8次

多对多映射

在平常的工作中,一个员工可能负责多个项目,同时一个项目会由多个员工协同完成。这就是多对多的模型。

所以这篇文章就以上述模型进行演示。

数据库表创建

create table project
(
    proid int primary key,
    proname varchar(20) not null
);

create table employee
(
    empid int primary key,
    empname varchar(20)
);

create table proemp
(
    rproid int,
    rempid int
);

alter table proemp add constraint fk_rproid 
    foreign key (rproid) references project(proid);
alter table proemp add constraint fk_rempid
    foreign key (rempid) references employee(empid);    

创建Java Bean

/**
 * 员工类
 */
public class Employee {

    private int empId;
    
    private String empName;
    
    private Set<Project> projects = new HashSet<>();

    // 省略setter/getter
}
/**
 * 项目类
 */
public class Project {

    private int proId;
    
    private String proName;
    
    private Set<Employee> employees = new HashSet<>();

    // 省略setter/getter
}

映射文件

Project.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.whyalwaysmea.hibernate.many2many.Project" table="project">
        <id name="proId" column="proid" type="java.lang.Integer">
            <generator class="assigned"></generator>
        </id>
        <property name="proName" type="java.lang.String">
            <column name="proname" length="20" not-null="true"></column>
        </property>
        <!-- 配置多对多关联关系 -->
        <set name="employees" table="proemp" cascade="all">
            <key column="rproid"></key>
            <many-to-many class="com.whyalwaysmea.hibernate.many2many.Employee" column="rempid"></many-to-many>
        </set>
    </class>
    
</hibernate-mapping>

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.whyalwaysmea.hibernate.many2many.Employee" table="employee">
        <id name="empId" column="empid" type="java.lang.Integer">
            <generator class="assigned"></generator>
        </id>
        <property name="empName" type="java.lang.String">
            <column name="empname" length="20" not-null="true"></column>
        </property>
        <!-- 配置多对多关联关系 -->
        <set name="projects" table="proemp" inverse="true">
            <key column="rempid"></key>
            <many-to-many class="com.whyalwaysmea.hibernate.many2many.Project" column="rproid"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

hibernate.cfg.xml就比较简单了,和以前的配置没有什么区别。只是记住加入上面的两个配置文件就可以了

<!-- 多对多映射 -->
<mapping resource="com/whyalwaysmea/hibernate/many2many/Employee.hbm.xml"/>
<mapping resource="com/whyalwaysmea/hibernate/many2many/Project.hbm.xml"/>

简单测试

/**
 * 因为在Employee.hbm.xml中设置了inverse="true",所以将控制权教给Project
 * 同时在Project.hbm.xml中设置了cascade="all" 所以保存的时候只用保存Project
 */
@Test
public void saveTest() {
    Employee garen = new Employee(1, "盖伦");
    Employee luminosity = new Employee(2, "拉克丝");
    
    Project mid = new Project(2, "中路");
    Project sup = new Project(3, "辅助");
    
    // 盖伦和拉克丝都在中路
    mid.getEmployees().add(garen);
    mid.getEmployees().add(luminosity);
    // 拉克丝又在辅助
    sup.getEmployees().add(luminosity);
    
    session.save(mid);
    session.save(sup);
}

@Test
public void findTest() {
    Project project = (Project) session.get(Project.class, 2);
    System.out.println(project.getProName() + "---" + project.getEmployees());
    
    Employee employee = (Employee) session.get(Employee.class, 1);
    System.out.println(employee.getEmpName() + "---" + employee.getProjects());
}

参考

Hibernate初探之多对多映射
相关代码

相关文章

网友评论

      本文标题:Hibernate初探之多对多映射

      本文链接:https://www.haomeiwen.com/subject/sfhsoxtx.html