JPA拼接子查询语句

public static Specification<CatDomain> listAdvanceSpec() {
        return (Root<CatDomain> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
           
      //创建主查询条件对象('where'后的语句对象)
      Predicate predicate = cb.conjunction();
      //创建子查询对象subQuery Subquery subQuery = query.subquery(String.class);
      //创建"实体类Hobby"的root对象

      Subquery subQuery = query.subquery(String.class);
      Root from = subQuery.from(IntentionRecord.class);
//创建子查询条件对象('where'后的语句对象)
      Predicate predicate1 = cb.conjunction();
predicate1 = cb.and(predicate1,cb.greaterThanOrEqualTo(from.get("createTime"), newDate2));
predicate1 = cb.and(predicate1,cb.equal(from.get("schoolId"), dto.getSchoolId()));
      //完成子查询
subQuery.select(from.get("studentId")).where(predicate1);
    //把子查询结果拼接到原查询语句后面---这里是id not in的写法
       predicate = cb.and(predicate, cb.not(cb.in(root.get("id")).value(subQuery)));
      //id in的写法

     
predicate = cb.and(predicate, cb.in(root.get("id")).value(subQuery));
      //id equals的写法      
      
, cb.equal(root.get("id"),"123"));                        }; }

 



posted @ 2019-03-07 14:43  闭眼的鱼  阅读(6377)  评论(1编辑  收藏  举报