Hello!
I want to use both schema validation (hibernate.hbm2ddl.auto=validate
) and NumericBooleanConverter
converter.
But when I run my code I get the following exception:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [activated] in table [todolist.activity]; found [int2 (Types#SMALLINT)], but expecting [integer (Types#INTEGER)]
Is it a bug or feature?
Hibernate: 6.6.0.Final
Postgresql: 42.7.3
Entity (Kotlin):
@Entity
@Table(name = "activity", schema = "todolist")
open class Activity(
@Id
@Column(name = "id", nullable = false)
open var id: Long? = null,
@Column(name = "uuid", nullable = false, length = Integer.MAX_VALUE)
open var uuid: String? = null,
@Convert(converter = org.hibernate.type.NumericBooleanConverter::class)
@Column(name = "activated", nullable = false)
open var activated: Boolean? = null,
)
SQL (Postgres):
create table activity
(
id bigint generated always as identity
constraint activity_pk
primary key,
uuid text not null,
activated smallint not null,
);
NumericBooleanConverter
creates an Integer
that has to be stored on the database side. By default, Hibernate expects an integer
typed column for that, hence the schema validation error you’re seeing.
Thank you for your answer!
I understand that. I would like to see a cast of types. As in runtime, but in the validation phase. (Perhaps it would be possible to somehow explicitly mark the type to which it should be cast)
Maybe it’s impossible on Hibernate’s part, or maybe it’s just not by design.
Could you (Hibernate team) fix that? If the answer is no, then why?
There is nothing to fix: Hibernate only knows that the java type Integer
has to be stored on the database side, and the default mapping for that in the SQL space is Types#INTEGER
.
You could use a custom @JdbcType
on your converted boolean property which defaults to the Types.SMALLINT
type-code.
1 Like
Your advice helps me to solve my issue! Thanks a lot!
The solution code:
@JdbcType(SmallIntJdbcType::class)
@Convert(converter = org.hibernate.type.NumericBooleanConverter::class)
@Column(name = "activated", nullable = false)
open var activated: Boolean? = null,
P.S. I really appreciate your work, both on the ORM, on its documentation and, no less important, your work with the community.
1 Like