After the migration of my project from spring boot 2 to spring boot 3 and updating the hibernate version to 6.1.7.Final. I’m encountering some problems with something that worked before.
Hibernate seems to ignore the insertion of the discriminator value on creation.
Example:
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = Book.BOOK_TABLE_NAME)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Book {
public static final String IDBOOK = "IDBOOK";
public static final String BOOK_TABLE_NAME = "BOOK";
@Id
@Column(name = IDBOOK)
private Long id;
@Column(name = "TYPE", insertable = false, updatable = false)
private String type;
....
}
@Entity
@DiscriminatorOptions(force = true)
@DiscriminatorValue("PERIODIC")
@Table(name = "PERIODIC")
@PrimaryKeyJoinColumn(name = "IDPERIODIC")
public class Periodic extends Book {
...attributes
}
@Entity
@DiscriminatorOptions(force = true)
@DiscriminatorValue("CODE")
@Table(name = "CODE")
@PrimaryKeyJoinColumn(name = "IDCODE")
public class Code extends Book {
...attributes
}
After executing the create method on the BookRepository and inspecting the generated code by hibernate I found out that is not adding the type value:
insert
into
book
(*"...many values except type"*, idbook)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
periodic
(..., idperiodic)
values
(?, ?, ?, ?, ?)
The result of this insert is the column type taking its default value in the database and ignoring the right type.
I tried removing the type attribute or removing the insertable = false so that hibernate will try to insert it. None of these worked.