代码之家  ›  专栏  ›  技术社区  ›  WendyG

标准生成器where子句

  •  0
  • WendyG  · 技术社区  · 6 年前

    我正在尝试使用criteriaBuilder和谓词构建一个SQL where子句,以创建如下SQL:

     WHERE
     (country5_.ctry_iso_cd = 'ESP'
    AND datadomain6_.data_dmn_cd = 'MTH'
    OR country5_.ctry_iso_cd = 'ESP'
    AND datadomain6_.data_dmn_cd = 'SOU'
    )
     AND NOT ( EXISTS (
         SELECT
             'NOT READY'
         FROM
             dc_dca_installation_status collection5_
         WHERE
             collection0_.installation_id = collection5_.installation_id
     ) )
    

    括号周围的AND或对是重要的,以分离它们与不存在。这是因为我得到的优先权是

    WHERE
     country5_.ctry_iso_cd = 'ESP'
    AND datadomain6_.data_dmn_cd = 'MTH'
    or country5_.ctry_iso_cd = 'ESP'
    AND datadomain6_.data_dmn_cd = 'SOU'
    AND NOT ( EXISTS (
         SELECT
             'NOT READY'
         FROM
             dc_dca_installation_status collection5_
         WHERE
             collection0_.installation_id = collection5_.installation_id
     ) )
    

    我可以建立所有的谓词没有问题,但我不知道如何让它在AND或pairs列表周围添加括号

    和或对的列表是这样构建到1个谓词列表中的

     countryDomainPredicates.add(cb.and(
         cb.equal(countryJoin.get(Country_.ctryIsoCd), ud.getCountryCode()),
         cb.equal(dataDomainJoin.get(DataDomain_.code), ud.getDataDomainCode())
    

    子查询是这样添加的

    cb.not(cb.exists(subquery));
    

    在哪里

    critQuery.where(cb.and(array));
    

    每个和或对周围的括号也一样好

    1 回复  |  直到 6 年前
        1
  •  0
  •   WendyG    6 年前

    我自己修复了它,而不是构建1个数组并将其转换为1个谓词。

    我将AND或OR数组转换成1个谓词,将不存在的谓词添加到另一个谓词中,然后使用2个谓词的数组到何处。

    List<Predicate> predicates = new ArrayList<>();
    predicates.add( addAndOrPairs(cb, root, userDataDomains));
    Subquery<String> subquery = createSubQuery(cb, root);
    predicates.add(cb.and(cb.exists(subquery).not()));
    critQuery.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));