javahibernatecomposite-primary-keynhibernate-inheritance

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


I use hibernate. I have an abstract class:

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

And inheritors

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

I want it to be possible not to specify the type of the property in the descendants of the class and all the properties were stored in one table.

Also, with the current implementation, an error crashes when starting the server: Repeated column in mapping for entity: com.timesheet.entity.project.property.ProjectCrossStaffingProperty column: type (should be mapped with insert="false" update="false")

I tried to specify the @Column annotation(insert="false" update="false") above the "type" field, but then when saving and retrieving data, the "type" field was specified as null and it was impossible to return a unique property.


Solution

  • As a result, I decided to make the Id a separate field, but add uniqueness to the table by the property of the Project Id and Type fields and pull out the desired property using an hql query, rather than using the session get method