Migrating from Hibernate 5 to Hibernate 6 I got an exception. I am querying an entity using a bind parameter departments that is a list:
var query = em.createQuery(
"from User u " +
"where COALESCE(:departments) is null or u.department in (:departments)",
User.class);
query.setParameter("departments", departments);
var result = query.getResultList();
To prevent empty IN clauses, I prepend the COALESCE(:departments) is null statement, as recommended on StackOverflow. When departments equals an empty list, Hibernate 5 converts the param to null that lets the database bypass the evaluation of the IN clause. Hibernate 6 behaves differently. The parameter ist converted to () that leads to a SQL syntax error in PostgreSQL:
I provided a minimal example project. |