Enum Query Parameter Mapping too strict: inconsistent handling of HQL queries

Description

Migration to From Hibernate 5 to 6 fails due more stringent handling of the enum mapping. Note: I am using PostgreSQL 16/17 databases.

Given: An entity class with an enum field and a long field.

 

 

The query

createQuery(“select x from EntityLog x where action = :action”).setParameter(“action”, “ACCESS”).getResultList();

fails in Hibernate 6 with the error message :

Error: Argument [ACCESS] of type [java.lang.String] did not match parameter type [com.benim.benim.enums.LogActionEnum (n/a)]]]

while it perfectly worked with Hibernate 5.

I have seen in other issue related to this topic that it is mentioned that the enum mapping is now more stringent and different workarounds are proposed.

However, i cannot understand this policy seeing that the following query stills work as in Hibernate 5:

createQuery(“select x from EntityLog x where entityId= :id”).setParameter(“id”, “1”).getResultList();

Obviously, in case of the long type, there is a default mapping from String to Long while in case Enum there is no default mapping anymore from String to the corresponding Enum value.

This is an unnecessary restriction which complicates heavily building generic query interfaces.

My use case is the following. I have lazy datatables build with primefaces where all the filter values are strings which comes from the user input. The generated query based on the primefaces lazy datatable filter values were working out of box. Now, for each different datatable based on an enum field one has to map the String filter values individually to the corresponding Java Enum type value. No more generic programming possible.

I want to bring your attention to the following curious observation: the query

createQuery(“select x from EntityLog x where action = ‘ACCESS’”).getResultList();

works while

createQuery(“select x from EntityLog x where entityId= ‘1’”).getResultList();

is not working.

So, for queries without parameters the mapping seems to be completely differently, even completely opposite to queries with parameters.

All four mentioned queries should run without errors. This would be my expected behavior. The last two queries can even directly mapped to a PostgreSQL database and will run without any issues.

Thanks in advance.

 

PS: Adding the column annotation does not help here, too!

Activity

Show:

Can Türker January 14, 2025 at 11:17 PM

would be highly appreciated if somebody could have a view into this issue and fix it. many thanks in advance.

Details

Assignee

Reporter

Labels

Worked in

Affects versions

Priority

Created January 10, 2025 at 1:54 AM
Updated January 14, 2025 at 11:17 PM