Set the NumericBooleanConverter as my default converter

Hi everyone, this is Alex, and I’m migrating from Hibernate 5.x to Hibernate 6.2.13.Final

In the migration guide, it states “In fact, if your application consistently maps booleans to the same database representation you can even register one as an auto-apply converter.”

I would like to set the NumericBooleanConverter as my default converter for all booleans. How can I do so? I’m using annotations and the persistence.xml file.

Kind regards.

You will have to define a custom class that extends the NumericBooleanConverter and annotate that with @Converter(autoApply = true), as well as add the FQN to that class to the persistence.xml as <class/> element.

Thanks a lot for your quick answer! I’ve created this class:

import jakarta.persistence.Converter;

@Converter(autoApply = true)
public class NumericBooleanConverter extends org.hibernate.type.NumericBooleanConverter {
}

I’ve also added this line to the persistence.xml file:

<class>com.telventi.healthcare.core.datamodel.NumericBooleanConverter</class>

But I sill get this error in my named queries when I startup the server:

Can’t compare test expression of type [BasicSqmPathSource(active : Boolean)] with element of type [basicType@5(java.lang.Integer,4)

What am I doing wrong?

Well, why do you try to compare a boolean attribute against an integer in the first place? Use a boolean literal/parameter instead.

We have literally thousands of queries already for hibernate 5 comparing boolean fields to 0 or 1. It would be unaffordable for us to review every one of them and change them.

If you’re comparing with literals i.e. where attribute = 1 then we might be able to support this, but I would very much advise you to fix your queries anyway. HQL always worked on the domain type system, so the type of the attribute is boolean. Hibernate ORM 5 was bad at type validation and comparing expressions just “happend to work”, even though the predicate was ill typed. ORM 6 fixes this type validation problems ORM 5 had and is much safer now.

Note that even if we consider supporting this for literals, this will never work for parameters, though I don’t believe this ever did work for parameters. I’ll discuss this matter today with the rest of the team and will let you know how we decided.

I highly appreciate it. I believe we always use booleans as parameters, but considering that in our database, booleans are stored as 0 and 1, people tend to write queries using 0 and 1 instead of true and false. I’d change the queries as you say, but it would be a titanic effort for us that might end up keeping us stuck in Hibernate 5.

I hope you all can find a way to work a way around this. Again, thanks a lot!

After discussing this today with my colleagues today, we decided against adding support for this again. You will have to change your HQL queries to use boolean literals instead of 0 or 1 if you want to use Hibernate ORM 6.

Ok, I’ll let the team know. Thanks for giving it a thought. I guess it’s time to review all the queries…

Kind regards