java - 使用@Embedded 和@Id 的Hibernate 复合主键

标签 java hibernate jpa hibernate-mapping

我想知道 hibernate 是否支持使用字段和组件类型的复合主键。所以我有一个 @Embeddable 组件类型,我想将它用作主键以及另一列作为复合主键。

所以我的表“DEPT_HISTORY”有复合主键(GROUP_DEPT、DEPTID、EFFDT)。我将 GROUP_DEPT 和 DEPTID 作为 @Embeddable 组件类型映射到 Department 类。

@Embeddable public class Department implements Serializable {

    private static final long serialVersionUID = 1L;

    private String departmentGroup;

    private String departmentId;

    public String getDepartmentGroup() {
        return departmentGroup;
    }

    public void setDepartmentGroup(String departmentGroup) {
        this.departmentGroup = departmentGroup;
    }

    public Department withDepartmentGroup(String departmentGroup) {
        setDepartmentGroup(departmentGroup);
        return this;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public Department withDepartmentId(String departmentId) {
        setDepartmentId(departmentId);
        return this;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("departmentGroup", getDepartmentGroup())
                .add("departmentId", getDepartmentId()).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Department)) {
            return false;
        }
        Department other = (Department) obj;

        return Objects.equal(getDepartmentGroup(), other.getDepartmentGroup())
                && Objects.equal(getDepartmentId(), other.getDepartmentId());

    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getDepartmentGroup(), getDepartmentId());
    }

}

我正在使用 GROUP_DEPT、上面的 DEPTID 和下面的 EFFDT 映射复合主键,如下所示。 hibernate 是否支持这个。我有它可以使用的类似类,但由于某种原因在此类上失败,原因是“由:org.hibernate.AnnotationException:com.blah.blah.component.Department 用作@EmbeddedId 时不得具有@Id 属性:com .blah.blah.entity.DepartmentHistory.部门

@Entity @Table(name = "dept_history") public class DepartmentHistory implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String DATETIME_FORMAT = "MM-dd-yyyy HH:mm:ss ZZ";
    protected static final DateTimeFormatter DATE_FORMAT = DateTimeFormat.forPattern(DATETIME_FORMAT);

    @Id
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "departmentGroup", column = @Column(name = "GROUP_DEPT", nullable = false)),
            @AttributeOverride(name = "departmentId", column = @Column(name = "DEPTID", nullable = false)) })
    private Department department;

    @Id
    @Column(name = "EFFDT", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar effectiveDate;

    @Column(name = "DESCR", nullable = false)
    private String description;

    @Column(name = "MANAGER_ID", nullable = false)
    private String managerId;

    public Department getDepartment() {
        return department;
    }

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

    public DepartmentHistory withDepartment(final Department department) {
        setDepartment(department);
        return this;
    }

    public Calendar getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(final Calendar effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public DepartmentHistory withEffectiveDate(final Calendar effectiveDate) {
        setEffectiveDate(effectiveDate);
        return this;
    }

    public DateTime readEffectiveDateAsDateTime() {
        return calendarToDateTime(effectiveDate);
    }

    public void writeEffectiveDateAsDateTime(final DateTime effectiveDate) {
        this.effectiveDate = dateTimeToCalendar(effectiveDate);
    }

    public DepartmentHistory withEffectiveDateAsDateTime(final DateTime effectiveDate) {
        writeEffectiveDateAsDateTime(effectiveDate);
        return this;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    public DepartmentHistory withDescription(final String description) {
        setDescription(description);
        return this;
    }

    public String getManagerId() {
        return managerId;
    }

    public void setManagerId(final String managerId) {
        this.managerId = managerId;
    }

    public DepartmentHistory withManagerId(final String managerId) {
        setManagerId(managerId);
        return this;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("department", getDepartment())
                .add("effectiveDate", DATE_FORMAT.print(readEffectiveDateAsDateTime()))
                .add("description", getDescription()).add("managerId", getManagerId()).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof DepartmentHistory)) {
            return false;
        }
        DepartmentHistory other = (DepartmentHistory) obj;

        return Objects.equal(getDepartment(), other.getDepartment())
                && Objects.equal(getEffectiveDate(), other.getEffectiveDate());

    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getDepartment(), getEffectiveDate());
    }

}

我可以使用 @Embedded 和字段上的 @Id 属性将 @Embeddable 和另一个字段组合成复合主键吗?我不想将 (GROUP_DEPT, DEPTID, EFFDT) 作为 @EmbeddedId,因为这样的组件类型没有意义,我不想创建一个在我的域中没有任何意义的类,只是用作复合主键。仅包含 (GROUP_DEPT, DEPTID) 的组件部门作为一个部门是有意义的。非常感谢。

最佳答案

根据规范,当您使用复合键时,您应该使用 @EmbeddedId 或 @IdClass。

If the dependent entity class has primary key attributes in addition to those corresponding to the parent's primary key or if the parent has a composite primary key, an embedded id or id class must be used to specify the primary key of the dependent entity. It is not necessary that parent entity and dependent entity both use embedded ids or both use id classes to represent composite primary keys when the parent has a composite key.

问题是:

I am mapping a composite primary key using GROUP_DEPT, DEPTID above and EFFDT below as follows. Does hibernate support this.?

是的,hibernate 支持它,但你应该使用 @EmbeddedId,我知道这需要创建一个新类来处理 key ,但据我所知,这需要完成。

关于java - 使用@Embedded 和@Id 的Hibernate 复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004428/

相关文章:

java - 当属性来自另一个表时,如何避免在 Spring Data JPA 上指定 SQL 查询

java - JPA中按组获取记录

Java - 如何仅读取输入流的某些部分

java - smartgwt - DynamicForm 相对列宽

java - 由于访问权限不足,无法添加 LDAP : error code 50 - The entry uid=test@info. com,ou=People,o=Drive,dc=company,dc=com

java - 在 hibernate 状态下替换父级的最佳方法

java - Hibernate/mysql 连接问题

java - Spring-security - 无法访问 ServletException

java - FlowLayout 中组件和容器边框之间的自由空间

java - Hibernate Search 集成到 Play Framework (JPA)