JPA 使用specification条件查询中distinct去重的用法

多表连接查询distinct去重

//分页及排序
pageInfo.setNumber(pageInfo.getNumber() - 1);
Pageable pageable = new PageRequest(pageInfo.getNumber(), pageInfo.getSize(), Sort.Direction.DESC, “id”);
//查询条件
Specification<User> specification = (root, cq, cb) -> {
List<Predicate> preList = new ArrayList<>();
// 模糊查询
if (StringUtils.isNotEmpty(name)) {
preList.add(cb.and(cb.like(root.get(“name”).as(String.class), “%”+name+"%")));
}
// distince去重
cq.distinct(true);
return cb.and(preList.toArray(new Predicate[preList.size()]));
};

sql是这样的:
SELECT DISTINCT
expert0_.ID AS ID1_5_,
expert0_.CREATED_TIME AS CREATED_2_5_,
expert0_.CREATOR AS CREATOR3_5_,
expert0_.ENABLED AS ENABLED4_5_,
expert0_.MODIFIED_TIME AS MODIFIED5_5_,
expert0_.REVISER AS REVISER6_5_,
FROM
T_TS_EXPERT expert0_
LEFT OUTER JOIN T_TS_EXPERT_APPOINTMENT expertappo1_ ON expert0_.ID = expertappo1_.EXPERT_ID
WHERE
expert0_.CREATOR =?

你可能感兴趣的:(JPA 使用specification条件查询中distinct去重的用法)