Hibernate - base class foreign key

Viewed 97

I have a Entity class Equipment which derives from base class RecordInfo:

@Entity
@DynamicInsert
@DynamicUpdate
@Getter
@Setter
@NoArgsConstructor
@Table(name="equipment", schema = "general",
        indexes = {@Index(name = "idx_general_equipment_deleted", columnList = "deleted")},
        uniqueConstraints= {
                @UniqueConstraint(name = "uq_general_equipment_code", columnNames = {"code"})
        })
public class Equipment extends RecordInfo implements ICodeDesc {

    @NotNull
    @Length(min = 1, max = 20)
    @Column
    private String code;

    @Length(max = 255)
    @Column
    private String description;

    @NotNull
    @Column(columnDefinition = "boolean default 'false'")
    private boolean deleted;

    @NotNull
    @Column
    @Enumerated(EnumType.ORDINAL)
    private EEquipmentType type;

    @ManyToOne(fetch = FetchType.LAZY)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name="unit_id", foreignKey = @ForeignKey(name = "fk_general_unit_equipment"))
    private Unit unit;
@MappedSuperclass
@Getter
@Setter
public class RecordInfo extends BaseData {

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @Column(name = "CREATED_AT", nullable = false)
    protected Instant createdAt;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @Column(name = "MODIFIED_AT", nullable = false)
    protected Instant modifiedAt;

    @Version
    @Column(name = "VER", nullable = false, columnDefinition="bigint default '0'")
    protected Long version;

    @ManyToOne(fetch= FetchType.LAZY)
    @JoinColumn(name = "MODIFIED_BY_ID", nullable = false, foreignKey=@javax.persistence.ForeignKey(name="fk_core_record_info_user"))
    protected UserData modifiedBy;

On application boot I get following exception :

org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl: GenerationTarget encountered exception accepting command : Error executing DDL "alter table general.equipment drop constraint fk_core_record_info_user" via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table general.equipment drop constraint fk_core_record_info_user" via JDBC Statement

Hibernate indeed deletes this foreign key earlier when it perfroms:

Hibernate: drop table if exists administration.user_data cascade

Relevant hibernate settings are:

hibernate.hbm2ddl.auto=create
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

I use Spring boot version '2.2.0.M6'

Is there a way to avoid this exception and how does hibernate determines order in which it drops and recreates tables on application boot?

0 Answers
Related