我有以下标准生成器语句:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<TestEntity> criteriaQuery = criteriaBuilder.createQuery(TestEntity.class);
Root<TestEntity> itemRoot = criteriaQuery.from(TestEntity.class);
criteriaQuery.select(itemRoot);
javax.persistence.criteria.CriteriaBuilder.In inClause = criteriaBuilder.in(itemRoot.get("cost"));
inClause.value(criteriaBuilder.literal(5.0d));
javax.persistence.criteria.CriteriaBuilder.In inClause2 = criteriaBuilder.in(itemRoot.get("cost"));
inClause2.value(criteriaBuilder.literal(5.0d));
Predicate pred = criteriaBuilder.or(inClause, criteriaBuilder.and(inClause2, criteriaBuilder.like(itemRoot.get("name"), "%name%")));
criteriaQuery.where(pred);
TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
List<TestEntity> list = typedQuery.getResultList();
它生成以下sql:
select *
from test_entity testentity0_
where
testentity0_.cost in (
5.0
)
or (
testentity0_.cost in (
5.0
)
)
and (
testentity0_.name like ?
)
但是where子句是错误的,我希望如下:
select *
from test_entity testentity0_
where
testentity0_.cost in (
5.0
)
or (
testentity0_.cost in (
5.0
)
and (
testentity0_.name like ?
)
)
和放错地方了,它应该放在或语句的第二个表达式中
由于and
运算符优先于or
:,因此这两个表达式是等价的
A or B and C
相当于A or (B and C)