Hello!
I'm having a very specific issue with Hibernate (5.2.1.Final), I'm starting to think it's a bug, but I don't want to rush the report. (Also the tracker site is kinda unfriendly, so I'm not sure it wouldn't be a duplicate.) So, to get to the point, here's the test code that throws an exception, while I don't think it should:
Code:
@Test
public void paramAdditionTest() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
// Entity can be anything, doesn't matter here
CriteriaQuery<Stuff> criteriaQuery = criteriaBuilder.createQuery(Stuff.class);
Root<Stuff> root = criteriaQuery.from(Stuff.class);
ParameterExpression<Integer> p1 = criteriaBuilder.parameter(Integer.class);
ParameterExpression<Integer> p2 = criteriaBuilder.parameter(Integer.class);
ParameterExpression<Integer> p3 = criteriaBuilder.parameter(Integer.class);
Predicate predicate = criteriaBuilder.equal(criteriaBuilder.sum(p1, p2), p3);
criteriaQuery.select(root).where(predicate);
TypedQuery<Stuff> query = entityManager.createQuery(criteriaQuery);
query.setParameter(p1, 1);
query.setParameter(p2, 2);
query.setParameter(p3, 3);
query.getResultList();
entityManager.getTransaction().commit();
entityManager.close();
}
This should run a query along these lines:
Code:
SELECT * FROM Stuff WHERE 1+2=3;
Simple, yet, it throws an exception:
Code:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
As a part of a more complex application, running in WildFly:
Code:
java.lang.IllegalArgumentException: Parameter value [3] did not match expected type [java.lang.Double (n/a)]
Same thing, basically. I did some digging, and I think the issue is that "somewhere" Hibernate fails to "port" the types of the parameters (while it's clearly possible with the getJavaType() method), so BinaryArithmeticOperatorNode fails to infer the result type (somewhere here:
https://github.com/hibernate/hibernate-orm/blob/ec4f20a5fba79444dbd704f1305f961c339dd7d1/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/BinaryArithmeticOperatorNode.java#L106).
Also, if any of the parameters comes from a "different source", that is on of the fields of the entity, like the id, the type is inferred just fine and the query executes. So I believe this is an issue with parameter handling?
Is this really is a bug, or am I doing something wrong?