简体   繁体   English

条件构建器where子句

[英]Criteria builder where clause

I am trying to build a SQL where clause using criteriaBuilder and predicates to create some SQL like: 我正在尝试使用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
 ) )

the brackets around the list of AND OR pairs are important to separate them from the not exists. 与或对对列表中的方括号对于将其与不存在分隔开来很重要。 This is because of the AND having precedence what i get is 这是因为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
 ) )

I can build up all the predicates no problem but I just don't know how to get it to add the brackets in around the AND OR pairs list 我可以毫无疑问地建立所有谓词,但我只是不知道如何在AND OR对列表周围添加方括号

the list of AND OR pairs are build like this into 1 list of predicates 这样将AND OR对列表构建为1个谓词列表

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

and the subquery is added like this 并像这样添加子查询

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

and the where 而哪里

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

Brackets around each AND OR pair would be just as good 每个“或”或“或”对之间的括号都一样

I fixed it myself, instead of building up 1 array and converting that into 1 predicate. 我自己修复了它,而不是建立1个数组并将其转换为1个谓词。

i converted the array of AND OR pairs into 1 predicate, added the not exists into another predicate and then used this array of 2 predicates into the where. 我将AND OR对对数组转换为1个谓词,将不存在添加到另一个谓词中,然后将2个谓词的数组用于where。

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()])));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM