Hibernate多对多双向关联的配置(3)

public class Emploee {
   
    private Integer empId;
    private String empName;
    private Set<ProEmp> proemp=new HashSet<ProEmp>(); //集合的类型为中间的实体类类型
    public Set<ProEmp> getProemp() {
        return proemp;
    }
    public void setProemp(Set<ProEmp> proemp) {
        this.proemp = proemp;
    }
    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;
    }
   

}

package cn.manytomany.doubleanother;

import java.util.HashSet;
import java.util.Set;

public class Project {
    private Integer proId;
    private String proName;

//集合的类型依然为中间的实体类类型
    private Set<ProEmp> proemp=new HashSet<ProEmp>();


    public Set<ProEmp> getProemp() {
    return proemp;
  }
    public void setProemp(Set<ProEmp> proemp) {
    this.proemp = proemp;
  }
  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;
  }

}


 

1.1  补充的中间实体类

package cn.manytomany.doubleanother;

public class ProEmp {
   
    private Integer id;
    private Emploee emp;
    private Project pro;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Emploee getEmp() {
        return emp;
    }
    public void setEmp(Emploee emp) {
        this.emp = emp;
    }
    public Project getPro() {
        return pro;
    }
    public void setPro(Project pro) {
        this.pro = pro;
    }
   
}

2. 接下来就是小配置了,跟第一个方案格式几乎是一样的,就不过多解释了,直接来看小配置就行了。

因为我们要用中间实体类来关联,所以雇员类(Emploee)和工程类(Project)没有什么眼添加的,只需按照正常的配置即可。

2.1 emploees.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.manytomany.doubleanother">
    <class table="Emploee">
        <id>
        <generator>
        <param>SQU_NUM</param>
        </generator>
        </id>
        <property></property>
    </class>
</hibernate-mapping>

2.2  emploees.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.manytomany.doubleanother">
    <class table="PROJECT">
        <id>
        <generator>
        <param>SQU_NUM</param>
        </generator>
        </id>
        <property></property>
    </class>
</hibernate-mapping>

2.3 关键就在于 proemp.hbm.xml  (把多对多关联转化成两个多对一来关联)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/0ec0eead0765c20ae8f00f5bb4e0f052.html