Search code examples
spring-boothibernatekotlinjpaspring-data-jpa

ID Column gets not created when using @Inheritance. "Column 'id' is an identity column defined as GENERATED ALWAYS"


I have this class

@Entity(name = "foo")
@Inheritance(strategy = InheritanceType.JOINED)
abstract class Base(
    @Enumerated(EnumType.STRING)
    val someType: SomeType
) {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    val id: Long = 0

    @Column
    val created: Date = Date()

    @Column
    val updated: Date = Date()
}

Which I use here

@Entity
@Table(name = "bar")
class Bar(
    @Column var a: Double? = null,
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "some_id")
    val cat: Cat
) : Base(
    someType = SomeType.variable_amount
)

WhenI try to do an INSERT I get the following error

[Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement [ERROR: cannot insert into column "id" Detail: Column "id" is an identity column defined as GENERATED ALWAYS. Hint: Use OVERRIDING SYSTEM VALUE to override.]

Why?


Solution

  • I created the table bar with pgAdmin and the column id was of type bigint. Apparently, it automatically add an IDENTIDY CONSTRAINT (not sure why, maybe the column id was of bigserial before).

    ALTER TABLE public.bar ALTER COLUMN id DROP IDENTITY;
    

    drops the CONSTRAINT and the INSERT triggered by the @Inheritance works!