The MySQL TIME() function returns a String, as is documented here:
Extracts the time part of the time or datetime expression expr and returns it as a string.
Therefore, one would expect this CriteriaQuery logic, which is doing a comparison between two String values, to work:
However, when executing that query Hibernate throws this exception:
Note that I've explicitly specified String.class as the function's return type. And in fact, if you replace the function name "TIME" with something else (e.g., "FOOBAR") the query compiles just fine. So Hibernate must have some built-in assumption that the TIME() function on every database returns a value of type Date, not String.
But obviously that's incorrect in the case of MySQL.
So it seems like one of the following fixes is needed here:
Disable any built-in implicit assumptions about the TIME() function having type Date - i.e., always trust what the caller says is the function's return type in CriteriaBuilder.function().
Move the TIME() function's return type assumption into the dialect
I have a test case here: