jpa specification 实现外连接

见(digitalgd)
一、调用
Specification spec;
PredicateBuilder<HonestTalk> pb = Specifications.<HonestTalk>and()
.eq(StringUtils.isNotBlank(sponsorOaAccount), "sponsorAccount", sponsorOaAccount)
.eq(StringUtils.isNotBlank(oaAccount), "oaAccount", oaAccount)
.eq(status != null, "status", status)
.predicate(StringUtils.isNotBlank(talkedOaAccount), this.getDetailSpecification(talkedOaAccount));
String currentUserAccount = CurrentUser.getCurrentUser().getUser().getAccount();
if (this.commonService.checkHasRole(currentUserAccount, DataScopeRole.MONITOR.name())) {
spec = pb.build();
} else {
spec = pb.build().and(dataScopeConditionGeneratorService.generateDataScopeCondition(currentUserAccount));
}
Page<HonestTalk> honestConversationPage = repository.findAll(spec, PageHelper.generatePageRequest(page, size, sort));

二、定义
public Specification<HonestTalk> getDetailSpecification(String talkedOaAccount) {
return (root, criteriaQuery, criteriaBuilder) -> {
// http://localhost:8080/student?companyName=北京&pageNumber=0&pageSize=5&name=明
/*
// 普通的or
Predicate namePre = criteriaBuilder.like(root.get("name"), "%" + paramRequest.getName() + "%");
Predicate companyPre = criteriaBuilder.like(root.get("company").get("name"), "%" + paramRequest.getCompanyName() + "%");
return criteriaBuilder.or(namePre, companyPre);
*/

// 使用左连接查询
Join<HonestTalk, HonestTalkDetail> talkDetailJoin = root.join("talkDetails", JoinType.INNER);

// Predicate namePre = criteriaBuilder.like(root.get("name"), "%" + paramRequest.getName() + "%");
Predicate detail = criteriaBuilder.equal(talkDetailJoin.get("talkedOaAccount"), talkedOaAccount);

// return criteriaBuilder.or(namePre, companyPre);
return detail;

};
}

jpa specification 实现外连接

上一篇:CATIA的后处理


下一篇:JDK配置环境变量