1.主从表
主表A与从表B关联:
若存在1:N关系,查询条件为B表字段写法为:
list.add(criteriaBuilder.like(root.joinList("projectContacts").get("pcontacts").as(String.class), "%" + search.getPcontacts() + "%"));
如果是1:1关系,写法如下:
roleConditions.add(cb.equal(root.join("projectThin", JoinType.INNER).get("industryType").as(String.class), roleindustry.getIndustryCode()));
如果表A与B关系为a.***=B.** and a.***=B.***,则写法为:
ListJoin<UvPcmerchants, List<UtMerFeedback>> taskJoin = root.joinList("merFeedbackList", JoinType.LEFT);
list.add(cb.and(cb.equal(taskJoin.get("depid"), par.getUserDept()),cb.notEqual(taskJoin.get("status"),"N")));
2.避免多次关联同一个表的情况,可按如下写法:
Join<UtProjectReport, UtProjectThin> thinJoin = root.join("projectThin", JoinType.INNER);
list.add(cb.equal(thinJoin.get("issubmit").as(String.class), "3"));
list.add(cb.notEqual(thinJoin.get("pstatus").as(String.class), "zz"));
list.add(cb.notEqual(thinJoin.get("pstatus").as(String.class), "fz"));
翻译后的SQL语句为:
INNER JOIN ut_project utprojectt2_ ON utprojectr0_.pid = utprojectt2_.PID
WHERE
uvprojectr1_.PRC_STATUS =?
AND uvprojectr1_.PRC_TYPE =?
AND utprojectt2_.ISSUBMIT =?
AND utprojectt2_.PSTATUS <>?
AND utprojectt2_.PSTATUS <>?