Hi,
After upgrading to Hibernate 6, my application does not start due to a schema validation error.
Schema-validation: wrong column type encountered in column [status]
in table [Usr]; found [int4 (Types#INTEGER)],
but expecting [smallint (Types#TINYINT)]
The User entity has an enum property “status”, and the type of the column “status” in PostgreSQL is an integer.
This code works with Hibernate 5 but does not work with Hibernate 6.
I tried to force the type with a “columnDefinition” like below but that does not work:
@Entity
@Table(name = "Usr")
public class User {
...
@NotNull
@Enumerated(EnumType.ORDINAL)
@Column(columnDefinition = "integer")
private UserStatus status;
...
}
public enum UserStatus {
OK, UNAUTHORIZED
}
Is it a bug?
Hibernate 6.1 changed the implicit SQL datatype for mapping enums from TINYINT
to SMALLINT
I don’t know if the @Column(columnDefinition = "integer")
is supposed to work, I will change the type of column.
I ran into this issue with my Postgres DB too. Indeed, Hibernate 6.1 changed the default type from TINYINT (1 byte) to SMALLINT (2 bytes). I’m confused why this causes the issue you and I have seen, where a column was priviously declared as a Postgres integer
type (4 bytes) and working fine, and now Hibernate wants it to be a smallint
(2 bytes). Postgres doesn’t seem to have a 1 byte integer type, so maybe it was defaulting to the regular integer
(4 byte) type?
I got columnDefinition to work when I used the correct type names. Looking at the error message closely, I see that my existing column is type int4
(this must be Hibernate’s name for it, as Postgres doesn’t have a type with that name). Hibernate was expecting it to be smallint
. So I set my columnDefinition to be what my table currently has:
@Column(columnDefinition = "int4")
Ah, I think the specific change that bit me wasn’t Hibernate 6.1 change, but a related Hibernate 6.2 change that made the column type automatically adapt to the number of elements in an enum.
The workaround with columnDefinition does not work for me.
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation:
wrong column type encountered in column [merchType] in table [Merch];
found [int (Types#INTEGER)],
but expecting [int4 (Types#SMALLINT)]
@Column(columnDefinition = "int4")
private MerchType merchType;
Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.