Closed dazey3 closed 2 years ago
I would also add that you can rewrite these unsupported examples using CriteriaBuilder.Case
Unsupported example 2 rewritten:
ParameterExpression<Integer> checkParam1 = cb.parameter(Integer.class);
ParameterExpression<Integer> checkParam2 = cb.parameter(Integer.class);
ParameterExpression<Integer> resultParam1 = cb.parameter(Integer.class);
ParameterExpression<Integer> resultParam2 = cb.parameter(Integer.class);
ParameterExpression<Integer> resultParam3 = cb.parameter(Integer.class);
Expression<Object> selectCase = cb.selectCase()
.when(cb.equal(root.get(SimpleEntity_.intVal2), checkParam1), resultParam1)
.when(cb.equal(root.get(SimpleEntity_.intVal2), checkParam2), resultParam2)
.otherwise(resultParam3);
cquery.where(cb.equal(root.get(SimpleEntity_.intVal1), selectCase));
Query query = em.createQuery(cquery);
query.setParameter(checkParam1, 2);
query.setParameter(checkParam2, 4);
query.setParameter(resultParam1, 20);
query.setParameter(resultParam2, 40);
query.setParameter(resultParam3, 60);
I believe both ways of writing should be considered equivalent.
this is done, thanks!
Looking at the JPA API for CriteriaBuilder.SimpleCase<C,R>, there is no available support for Expressions to be passed as the condition of a WHEN clause.
There are currently only two ways to formulate a WHEN clause using the CriteriaBuilder.SimpleCase API:
Notice that both methods only accept a condition of type
C
. The result can be of type Expression<? extends R>, but the condition must be of type C. I believe this is limiting and creates usecases that a user would expect to work, but are not supported.A currently supported example:
A very simple example that passes constant values into the CASE select expression.
Unsupported example 1:
Unsupported example 2:
I think the lack of API methods in CriteriaBuilder.SimpleCase<C,R> can very easily expand to include:
This allows expression values (parameter and literal) to be passed as one would expect it to work