Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用@Embedded和@Id休眠复合主键_Java_Hibernate_Jpa_Hibernate Mapping - Fatal编程技术网

Java 使用@Embedded和@Id休眠复合主键

Java 使用@Embedded和@Id休眠复合主键,java,hibernate,jpa,hibernate-mapping,Java,Hibernate,Jpa,Hibernate Mapping,我想知道hibernate是否支持使用字段和组件类型的复合主键。因此,我有一个@Embeddeble组件类型,我想将它用作主键,并将另一列用作复合主键 因此,我的表“DEPT\u HISTORY”具有复合主键(GROUP\u DEPT、DEPTID、EFFDT)。我将GROUP_DEPT和DEPTID作为@embedded组件类型映射到Department类 @Embeddable public class Department implements Serializable { pr

我想知道hibernate是否支持使用字段和组件类型的复合主键。因此,我有一个@Embeddeble组件类型,我想将它用作主键,并将另一列用作复合主键

因此,我的表“DEPT\u HISTORY”具有复合主键(GROUP\u DEPT、DEPTID、EFFDT)。我将GROUP_DEPT和DEPTID作为@embedded组件类型映射到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());
    }

}
我使用groupdept、DEPTID和EFFDT映射一个复合主键,如下所示。hibernate是否支持这一点。我有类似的类,它可以工作,但由于某种原因,在这个类上失败,原因是“Caused by:org.hibernate.AnnotationException:com.blah.blah.component.Department用作@EmbeddedId:com.blah.blah.entity.DepartmentHistory.Department时不能有@Id属性

@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 and字段上的@Id属性将@Embedded和另一个字段组合成复合主键。我不想做(GROUP_DEPT、DEPTID、EFFDT)作为@EmbeddedId,因为这样的组件类型没有意义,我不想创建一个在我的域中没有任何意义的类,只是用作复合主键作为一个部门来说是有意义的。非常感谢。

根据使用复合密钥时应该使用的规范和@EmbeddedId或@IdClass

如果从属实体类还具有主键属性 与父项的主键相对应,或者如果父项 如果具有复合主键,则必须使用嵌入式id或id类 指定依赖实体的主键。它不是 父实体和从属实体都使用嵌入式 当出现以下情况时,id或两者都使用id类来表示复合主键 父项有一个复合键

问题是:

我正在使用上面的GROUP_DEPT和DEPTID映射复合主键 和下面的EFFDT。hibernate支持吗


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

感谢您的响应。我没有在这里实现任何继承,因此没有父实体和依赖实体。我有类似的类,例如location和location history,其中c组件类型位置为(组位置,位置)EFFDT作为实体位置_历史记录中的一个字段。我以完全相同的方式映射了它,它工作得很好。只是这个特定的类似乎不起作用,我无法找出原因。只是一个问题,当您尝试使用EntityManager find方法基于键检索对象时,您的模型工作?此模型在上述exc中失败当spring尝试实例化bean时,我有一个类似的位置模型,当我执行location location=new location().withLocationGroup(“TEST”).withLocationId(“100”);List locationHistories=locationHistoryDao.getLocationHistoryById(location);在dao中我使用criteria api来获得结果