Please see the attached test cases.
For some specific queries Hibernate fails to infer the correct parameter type and throws an exception when trying to get the result. I'm using the JPA Crtieria API. I've identified two cases that I think are somehow related.
First when trying to execute a query that only contains parameters, like " ... WHERE 1 + 2 = 3". (I'm building queries dynamically from client request, that's how I can end up with such expressions.) Snippet from first test case:
Hibernate fails to infer the type, while it's clearly available in the parameter (getJavaType()). It guesses Double (https://github.com/hibernate/hibernate-orm/blob/0a2a5c622e3eb30724e80bc8661c0ac55ebfb2be/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/BinaryArithmeticOperatorNode.java#L106), and fails to cast. I don't think the guessing part is wrong, but that the type info is lost "somewhere along the way".
I tried running similar queries with Hibernate's API (not quite familiar with it), using the two org.hibernate.query.Query.setParameter() methods, and it only works when I used the one where the type is declared explicitly. So if the JPA calls are "mapped" to the Hibernate API than it might be an issue with the mapping.
The other issue, which I think is related is the inconsistencies in inference capabilities. When comparing numeric fields and/or parameter, the following can be observed (see second test case for actual code):
int_param < long_param --> works
int_field < long_field --> works
int_param < long_field --> exception
Also, the related forum discussion, from a month ago: https://forum.hibernate.org/viewtopic.php?f=1&t=1043600 Not much more info there though.