Use JDBC bind variables for handling JPA Criteria query numeric literals

Description

A predicate with a String literal criteriaBuilder.equal(from.get("name"), "horse"); creates a bind parameter: name=aram. Fine!

A predicate with a numeric literal criteriaBuilder.equal(from.get("age"), Integer.valueOf(42)); does not create a bind parameter: age=42. Not fine!

The concern here is that the fact that literals are used in the JDBC query rather than bind variables (parameters) a PreparedStatement cannot be effectively cached.

I think caused this issue.

Test Case

Animal.java

1 2 3 4 5 6 7 @Entity public class Animal { @Id Long id; String name; Integer age; }

CriteriaParameterTest.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @Entity @Test public void testPredicateWithString() { EntityManager entityManager = getEntityManager(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Animal> criteriaQuery = criteriaBuilder.createQuery(Animal.class); Root<Animal> from = criteriaQuery.from(Animal.class); Predicate namePredicate = criteriaBuilder.equal(from.get("name"), "horse"); criteriaQuery.select(from).where(namePredicate); TypedQuery<Animal> typedQuery = entityManager.createQuery(criteriaQuery); Query query = typedQuery.unwrap(QueryImpl.class).getHibernateQuery(); System.out.println(query.getQueryString()); assertTrue(query.getQueryString().contains("name=:param")); } @Test public void testPredicateWithInteger() { EntityManager entityManager = getEntityManager(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Animal> criteriaQuery = criteriaBuilder.createQuery(Animal.class); Root<Animal> from = criteriaQuery.from(Animal.class); Predicate agePredicate = criteriaBuilder.equal(from.get("age"), Integer.valueOf(42)); criteriaQuery.select(from).where(agePredicate); TypedQuery<Animal> typedQuery = entityManager.createQuery(criteriaQuery); Query query = typedQuery.unwrap(QueryImpl.class).getHibernateQuery(); System.out.println(query.getQueryString()); assertTrue(query.getQueryString().contains("age=:param")); } }

Environment

None

Status

Assignee

Andrea Boriero

Reporter

Kai Prünte

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Affects versions

5.0.6
4.3.8

Priority

Major
Configure