Search code examples
javaspringspring-boothibernatekotlin

Hibernate fails Integer schema validation after the update


I used to run spring boot v2.x but now after updating it to 3.1.4 and getting all the new dependencies, it appears there is this annoying thing which is preventing the application to run and fails on boot.

Basically, I have an entity that maps to a column of type integer in Postgres DB, and back to an enum in SpringBoot/Hibernate. The code used to look very simple, just like this:

@Column(name = "status") 
var status: StatusEnum,

status is of type Integer in DB, and this mapping worked perfectly. Then I updated spring boot version to 3.x, and now during both schema validation fails with this error:

nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [status] in table [xxx]; found [int4 (Types#INTEGER)], but expecting [smallint (Types#TINYINT)]"

I thought, OK, it is expecting smallint but it is an integer there, there must be a way to override this behavior, so I tried telling it with columnDefinition:

@Column(name = "status", columnDefinition = "INTEGER") 
var status: StatusEnum,

And error again:

Schema-validation: wrong column type encountered in column [status] in table [xxx]; found [int4 (Types#INTEGER)], but expecting [integer (Types#TINYINT)]"

I tried setting in columnDefinition other values as well, such as integer, int4, int2, doesn't matter always the same error.

Is it really possible that this default behavior looking for TINYINT cannot be overriden and I'll have to change my db defintion?

The dialect set in my application.yml is org.hibernate.dialect.PostgreSQLDialect if it is of any value.


Solution

  • We had the exact same issue in one of our services and the solution was to explicitly ask Hibernate not to do the validation. In our application.yaml file we added:

    ...
    spring:
      jpa:
        hibernate:
          ddl-auto: none
    ...