Handling Type Casting Issues in HQL Queries for Boolean Fields Across Different Databases

In my Java entity class, I have a Boolean field that corresponds to different data types in various databases. Specifically, in PostgreSQL, this field is of type boolean; in MySQL, it is tinyint; and in Oracle, it is NUMBER(3).

When executing HQL queries, I use a WHERE clause to compare this Boolean field to the integer value 1. For example, if the Boolean column is named checked, the WHERE clause is written as follows: WHERE checked = 1.

While this approach works for MySQL and Oracle, it results in an error in PostgreSQL: “operator does not exist: boolean = integer.”

Is there a way to handle implicit type casting to prevent such errors and ensure compatibility across all these databases?

HQL is not SQL. For HQL, a boolean is a boolean, so you simply write where checked = true or even just where checked if you use ORM 6+. Hibernate ORM will just translate the HQL to the corresponding SQL.