Hibernate多对多关联关系

项目和员工关系 一个项目需要多个员工参与,一位员工可能参与多个项目

单向多对多关联

  • 1.创建Project(项目)和Employee(员工)持久化类
  • 2.建立Project(项目)和Employee(员工)的单向多对多关联
  • 3.验证对象的持久化方法
package com.pojo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Project implements Serializable {
    private Integer proid;
    private String proName;
    private Set employees = new HashSet<>();

    public Project() {
    }

    public Project(Integer proid, String proName, Set employees) {
        this.proid = proid;
        this.proName = proName;
        this.employees = employees;
    }

    public Integer getProid() {
        return proid;
    }

    public void setProid(Integer proid) {
        this.proid = proid;
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public Set getEmployees() {
        return employees;
    }

    public void setEmployees(Set employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        return "Project{" +
                "proid=" + proid +
                ", proName='" + proName + '\'' +
                ", employees=" + employees +
                '}';
    }
}
package com.pojo;

import java.io.Serializable;

public class Employee implements Serializable {
    private Integer empid;
    private String empName;

    public Employee() {
    }

    public Employee(Integer empid, String empName) {
        this.empid = empid;
        this.empName = empName;
    }

    public Integer getEmpid() {
        return empid;
    }

    public void setEmpid(Integer empid) {
        this.empid = empid;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empid=" + empid +
                ", empName='" + empName + '\'' +
                '}';
    }
}



    
        
            
        
        
        
            
            
        
    

元素

  • table属性指定关系表的名称为proemp
  • cascade属性“save-update”表明保存或更新Project对象时,会级联保存或更新与它有关的Employee对象

元素

  • column属性指定proemp表的外键RPROID,用来参照pro表

元素

  • class属性指定employess集合中存放的是Employee对象
  • cloumn属性指定proemp表的外键REMPID,用来参照employee表

总结
对于多对多关联,cascade属性设为"save-update",如果设置为"all",如果删除一个Project对象会级联删除与他关联的所有Employee对象,显然这是不合理的。

public void save(Project project)
    {
        this.getCurrentSession().save(project);
    }
 public void save()
    {
        //创建两个项目
        Project project1=new Project();
        project1.setProid(1);
        project1.setProName("项目1");
        Project project2=new Project();
        project2.setProid(2);
        project2.setProName("项目2");
        //创建三个员工
        Employee employee1=new Employee(1,"张三");
        Employee employee2=new Employee(2,"李四");
        Employee employee3=new Employee(3,"王五");
        //进行单向多对多关联
        project1.getEmployees().add(employee1);
        project1.getEmployees().add(employee2);
        project2.getEmployees().add(employee1);
        project2.getEmployees().add(employee2);
        project2.getEmployees().add(employee3);
        proBiz.save(project1);
        proBiz.save(project2);

    }

Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
insert
into
project.pro
(proName, proid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
insert
into
project.pro
(proName, proid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)

总结
首先根据主键生成策略的assigned根据编号查询是否存在该员工的,之后向pro表中插入一条记录,也向employee插入一条记录,同时向中间表proemp表插入一条记录,由于与project2对象关联的employee1和employee2对象已经保存到employee表中所以不再向employee表插入记录。

双向多对多关联

  • 1.Employee(员工)持久化类
  • 2.建立Employee(员工)和Project(项目)的单向多对多关联
  • 3.验证对象的持久化方法
package com.pojo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Employee implements Serializable {
    private Integer empid;
    private String empName;
    private Set projects=new HashSet<>();

    public Employee() {
    }

    public Employee(Integer empid, String empName) {
        this.empid = empid;
        this.empName = empName;
    }

    public Employee(Integer empid, String empName, Set projects) {
        this.empid = empid;
        this.empName = empName;
        this.projects = projects;
    }

    public Integer getEmpid() {
        return empid;
    }

    public void setEmpid(Integer empid) {
        this.empid = empid;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Set getProjects() {
        return projects;
    }

    public void setProjects(Set projects) {
        this.projects = projects;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empid=" + empid +
                ", empName='" + empName + '\'' +
                ", projects=" + projects +
                '}';
    }
}




    
        
            
        
        
        
            
            
        
    

总结
对于双向多对多关联的两端,需要把其中的一端的元素的inverse属性设置为"true"。

public void save()
    {
        //创建两个项目
        Project project1=new Project();
        project1.setProid(1);
        project1.setProName("项目1");
        Project project2=new Project();
        project2.setProid(2);
        project2.setProName("项目2");
        //创建三个员工
        Employee employee1=new Employee(1,"张三");
        Employee employee2=new Employee(2,"李四");
        Employee employee3=new Employee(3,"王五");
        //进行双向多对多关联
        
        project1.getEmployees().add(employee1);
        project1.getEmployees().add(employee2);
        
        project2.getEmployees().add(employee1);
        project2.getEmployees().add(employee2);
        project2.getEmployees().add(employee3);

        employee1.getProjects().add(project1);
        employee1.getProjects().add(project2);

        employee2.getProjects().add(project1);
        employee2.getProjects().add(project2);

        employee3.getProjects().add(project2);

        proBiz.save(project1);
        proBiz.save(project2);

    }

Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
insert
into
project.pro
(proName, proid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
select
employee_.empid,
employee_.empName as empName4_
from
project.employee employee_
where
employee_.empid=?
Hibernate:
insert
into
project.pro
(proName, proid)
values
(?, ?)
Hibernate:
insert
into
project.employee
(empName, empid)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)
Hibernate:
insert
into
proemp
(RPROID, REMPID)
values
(?, ?)

你可能感兴趣的:(Hibernate多对多关联关系)