使用Spring-data-jpa执行嵌套查询时,语句中括号位置不正确
使用Spring-data-jpa执行嵌套查询时,语句中括号位置不正确,相关代码如下:
Specification<ProjectVO> specification = new Specification<ProjectVO>() {
private static final long serialVersionUID = -8267625374879507726L;
public Predicate toPredicate(Root<ProjectVO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<Predicate>();
if(!StringUtils.isEmpty(key)) {
predicates.add(cb.or(
cb.like(root.<String>get(ProjectTab.H_NAME), "%" + key + "%"),
cb.like(root.<String>get(ProjectTab.H_CUSTOMER).get(CustomerTab.H_NICKNAME), "%" + key + "%")
));
}
Join<ProjectVO, CustomerVO> customerJoin = root.join(ProjectTab.H_CUSTOMER, JoinType.LEFT);
predicates.add(cb.equal(root.<Integer>get(ProjectTab.H_TYPE), Project.TYPE_LAWSUIT));
if(service != null) {
predicates.add(cb.equal(root.<ServiceVO>get(ProjectTab.H_SERVICE), service));
}
if(projectStatus != null && projectStatus != 0) {
predicates.add(cb.equal(customerJoin.<StaffVO>get(CustomerTab.H_SALESMAN), salesman));
predicates.add(cb.equal(root.<Integer>get(ProjectTab.H_PROJECT_STATUS), projectStatus));
}else {
Predicate coursePredicate = cb.and(
cb.isNull(customerJoin.<StaffVO>get(CustomerTab.H_SALESMAN)),
cb.equal(root.<Integer>get(ProjectTab.H_PROJECT_STATUS), Project.PROJECT_STATUS_EXAMINE)
);
coursePredicate = cb.or(
coursePredicate,
cb.equal(customerJoin.<StaffVO>get(CustomerTab.H_SALESMAN), salesman)
);
predicates.add(coursePredicate);
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
如上代码,期望SQL的查询条件按如下方式执行:
select * from t_project projectvo0_
left outer join
t_user_customer customervo1_
on projectvo0_.c_customer=customervo1_.c_id
where
projectvo0_.c_type=1
and (
(
customervo1_.c_salesman is null
and projectvo0_.c_project_status=1
)
or customervo1_.c_salesman=?
)
order by
projectvo0_.c_modify_time desc limit ?
但实际却是
select * from t_project projectvo0_
left outer join
t_user_customer customervo1_
on projectvo0_.c_customer=customervo1_.c_id
where
projectvo0_.c_type=1
and (
(
customervo1_.c_salesman is null
)
and projectvo0_.c_project_status=1
or customervo1_.c_salesman=?
)
order by
projectvo0_.c_modify_time desc limit ?
在where语句中的括号位置没有对,请问如何正确的完成这个查询操作的书写呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请问搞定了吗,我也遇到这个问题?求指导
没有搞定。。。。难受