我正在尝试将查询转换为遇到问题的条件。以下是我要转换的查询
SELECT
machine_type_id,
machine_type,
user_name
FROM
nc_machine_types
WHERE
CASE
WHEN
TRIM(':q') = ''
OR ':q' IS NULL
THEN
machine_type IS NOT NULL
ELSE (machine_type = ':q'
OR machine_type_id = (SELECT
machine_type_id
FROM
nc_machine_screen
WHERE
machine_no = ':q'))
END
我现在可以转换的谓词是
return new Specification<MachineType>() {
@Override
public Predicate toPredicate(Root<MachineType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if(!StringUtils.isBlank(q)) {
Subquery<Long> ordersSubquery = query.subquery(Long.class);
Root rootB = ordersSubquery.from(Mahine.class);
ordersSubquery.select(rootB.get("machineType").get("machineTypeId"));
ordersSubquery.where(cb.equal(rootB.get("machineNo"), q));
predicates.add(cb.equal(root.get("machineType"), q));
cb.or(cb.equal(root.get("machineId"), ordersSubquery));
}
return cb.and(predicates.toArray(new Predicate[0]));
}
};
正在构造的查询就像
select machine_type_id, user_name, machine_type from machine_types where machine_type=?
查询中没有OR
子句。如何在查询中添加它?
答案 0 :(得分:0)
我得到了答案。我可以简单地删除
predicates.add(cb.equal(root.get("machineType"), q));
cb.or(cb.equal(root.get("machineId"), ordersSubquery));
并添加
cb.or(cb.equal(root.get("machineType"), q), cb.equal(root.get("machineId"), ordersSubquery));