Spring JPA标准生成器未生成正确的SQL



我有以下标准生成器语句:

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)

相关内容

  • 没有找到相关文章

最新更新