OPENJPA-2770 ensure equal(xxx, literal(boolean)) works in criteria builder

This commit is contained in:
Romain Manni-Bucau 2019-01-23 10:21:53 +01:00
parent 61b48040db
commit 9e222652df
3 changed files with 27 additions and 4 deletions

View File

@ -76,6 +76,16 @@ public class TestTypesafeCriteria extends CriteriaTest {
assertEquivalence(q.where(cb.literal(Boolean.FALSE)), FALSE_JPQL);
}
public void testBooleanLiteralInEquals() {
CriteriaQuery<Order> q = cb.createQuery(Order.class);
Root<Order> root = q.from(Order.class);
Path<Object> path = root.get("delivered");
Expression<Boolean> literal = cb.literal(Boolean.FALSE);
assertEquals( // we don't want o.delivered = 1 <> 1 but o.delivered = false
"SELECT o FROM Order o WHERE o.delivered = false",
((OpenJPACriteriaQuery<?>) q.select(root).where(cb.equal(path, literal))).toCQL());
}
public void testDefaultAndIsTrue() {
CriteriaQuery<Person> q = cb.createQuery(Person.class);
q.from(Person.class);

View File

@ -264,19 +264,19 @@ public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionPa
@Override
public <N extends Number> Expression<N> diff(Expression<? extends N> x,
Expression<? extends N> y) {
return new Expressions.Diff<>(x, y);
return new Expressions.Diff<>(replaceExpressionForBinaryOperator(x), replaceExpressionForBinaryOperator(y));
}
@Override
public <N extends Number> Expression<N> diff(
Expression<? extends N> x, N y) {
return new Expressions.Diff<>(x, y);
return new Expressions.Diff<>(replaceExpressionForBinaryOperator(x), y);
}
@Override
public <N extends Number> Expression<N> diff(N x,
Expression<? extends N> y) {
return new Expressions.Diff<>(x, y);
return new Expressions.Diff<>(x, replaceExpressionForBinaryOperator(y));
}
@Override
@ -288,7 +288,17 @@ public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionPa
public Predicate equal(Expression<?> x, Expression<?> y) {
if (y == null)
return new Expressions.IsNull((ExpressionImpl<?> )x);
return new Expressions.Equal(x, y);
return new Expressions.Equal(replaceExpressionForBinaryOperator(x), replaceExpressionForBinaryOperator(y));
}
private <T> Expression<T> replaceExpressionForBinaryOperator(final Expression<T> expression) {
if (expression == PredicateImpl.TRUE()) {
return (Expression<T>) PredicateImpl.TRUE_CONSTANT;
}
if (expression == PredicateImpl.FALSE()) {
return (Expression<T>) PredicateImpl.FALSE_CONSTANT;
}
return expression;
}
@Override

View File

@ -45,6 +45,9 @@ import org.apache.openjpa.kernel.exps.Literal;
* @since 2.0.0
*/
abstract class PredicateImpl extends ExpressionImpl<Boolean> implements Predicate {
static final Expression<?> TRUE_CONSTANT = new Expressions.Constant<>(true);
static final Expression<?> FALSE_CONSTANT = new Expressions.Constant<>(false);
private static Predicate TRUE;
private static Predicate FALSE;