如何使用Hibernate Annotations实现复合主键

时间:2014-12-27 11:15:59

标签: hibernate annotations

我想知道如何通过Hibernate注释方法创建复合组合。

我有3张员工,部门和工作表员工部-Juntion

由于我想将Employee和Department作为单独的表,我创建了一个Junction Table,以维持多对多关系。

我想知道如何为Department Table创建复合主键 对于列Center_Code&通过Hibernate Annotations Depart_Code。

请问有人帮我解决这个问题。

下面我还附上了Table Relation Ship和Java POJO Class。

表关系

Table Relationship

JAVA POJO CLASS

package com.hibernate;

import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.Id;

/**
 *
 * @author SPAR
 */
@Entity
public class Department {

    @Id
    private String cost_Center_Code;
    private String dep_Code;
    private String department;
    private String sub_Department;
    private String division;

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getDep_Code() {
        return dep_Code;
    }

    public void setDep_Code(String dep_Code) {
        this.dep_Code = dep_Code;
    }

    public String getCost_Center_Code() {
        return cost_Center_Code;
    }

    public void setCost_Center_Code(String cost_Center_Code) {
        this.cost_Center_Code = cost_Center_Code;
    }

    public String getDivision() {
        return division;
    }

    public void setDivision(String division) {
        this.division = division;
    }

    public String getSub_Department() {
        return sub_Department;
    }

    public void setSub_Department(String sub_Department) {
        this.sub_Department = sub_Department;
    }

    @Override
    public String toString() {
        return "Department{" + "cost_Center_Code=" + cost_Center_Code + ", dep_Code=" + dep_Code + ", department=" + department + ", sub_Department=" + sub_Department + ", division=" + division + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + Objects.hashCode(this.cost_Center_Code);
        hash = 79 * hash + Objects.hashCode(this.dep_Code);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Department other = (Department) obj;
        if (!Objects.equals(this.cost_Center_Code, other.cost_Center_Code)) {
            return false;
        }
        if (!Objects.equals(this.dep_Code, other.dep_Code)) {
            return false;
        }
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

我认为,您正在寻找的是EmbeddedId,您可以执行以下操作。

  1. 首先从您的Department课程中取出这些专栏,然后创建一个如下所示的新课程。

        @Embeddable    
        class CenterDeptId{
    
           @Column(name = "center_code")
           private String cost_Center_Code;
    
           @Column(name = "dep_code")
           private String dep_Code;
    
           // getter && setter 
    
       }
    
  2. 在Department类

    中使用上述类
    @Entity    
    public class Department {
    
        @EmbeddedId
        private CenterDeptId centerDeptId;
        private String department;
        private String sub_Department;
        private String division;
    
      //getter && setter
    
    }
    

答案 1 :(得分:1)

@Entity
public class Department {

    @EmbeddedId
    private DepartmentId departmentId

    private String department;
    private String sub_Department;
    private String division;
}

@Embeddable
public class DepartmentId {
    @Column(name = "")
    private String cost_Center_Code;

    @Column(name = "")
    private String dep_Code;

    //getter, setter methods
}