Hibernate master primary key using @Embedded and @Id

I wanted to find out if hibernate supports a composite primary key using the field and component type. Therefore, I have a component type @Embeddable, and I would like to use it as a primary key along with another column as a composite primary key.

So, my table "DEPT_HISTORY" has composite primary keys (GROUP_DEPT, DEPTID, EFFDT). I mapped GROUP_DEPT and DEPTID as the component type of @Embeddable with the department class.

@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 . , , - "Caused by: org.hibernate.AnnotationException: com.blah.blah.component.Department @Id- @EmbeddedId: com.blah.blah.entity.DepartmentHistory.department

@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());
    }

}

@Embeddable , @Id @Embedded . (GROUP_DEPT, DEPTID, EFFDT) @EmbeddedId, , , , . , (GROUP_DEPT, DEPTID), . .

+4
1

@EmbeddedId @IdClass .

, , , id id- . , ids id , .

:

, GROUP_DEPT, DEPTID EFFDT . hibernate .

, hibernate, @EmbeddedId, , , , .

+1

All Articles