@IdClass 与 @Inheritance(strategy = InheritanceType.SINGLE_TABLE)

问题描述 投票:0回答:1

我使用休眠。 我有一个抽象类:

@Data
@Entity
@Table(schema = "timesheetdb", name = "project_property")
@IdClass(ProjectPropertyPK.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class ProjectPropertyAbstract {

    @Id
    private Integer projectId;

    @Id
    private String type;
}
@Data
@Embeddable
public class ProjectPropertyPK implements Serializable {

    private Integer projectId;
    private String type;
}

还有继承者

@Data
@Entity
@DiscriminatorValue("WAGE_FUND")
public class ProjectWageFundProperty extends ProjectPropertyAbstract {

    private Boolean isEnabled;
    private Integer percentProfit;
}
@Data
@Entity
@DiscriminatorValue("CROSS_STAFFING")
public class ProjectCrossStaffingProperty extends ProjectPropertyAbstract {
    
    private Boolean isEnabled;
}

我希望能够在类的后代中指定属性的类型,并且所有属性都存储在一张表中。

此外,在当前的实现中,启动服务器时会出现错误崩溃:

Repeated column in mapping for entity: com.timesheet.entity.project.property.ProjectCrossStaffingProperty column: type (should be mapped with insert="false" update="false")

我尝试在“type”字段上方指定@Column注解(insert="false" update="false"),但是当保存和检索数据时,“type”字段被指定为null,无法返回一个独特的属性。

java hibernate composite-primary-key nhibernate-inheritance
1个回答
0
投票

无法将

DiscriminatorColumn
指定为属性,就像您在此处使用
type
所做的那样,因此出现了原始错误。如果您添加建议的注释,该值将为空,因为它不再保存或从数据库中获取。

解决方案是删除

type
属性(如果不需要)(通常不应该如此),或者保留注释并在每个子类中手动设置它。

public ProjectWageFundProperty() {
    setType("WAGE_FUND");
}
© www.soinside.com 2019 - 2024. All rights reserved.