我正在尝试使用JPA规范的类完成以下sql查询。
SELECT p.*
FROM Path p
WHERE p.path_id IN (
SELECT s2.path_id
FROM segment s2
INNER JOIN segment_end s3 ON s2.segment_id = s3.segment_id
WHERE s3.site_name = 'ABC' and s3.passive_flag=false)
AND p.path_id IN (
SELECT s2.path_id
FROM segment s2
INNER JOIN segment_end s3 ON s2.segment_id = s3.segment_id
WHERE s3.site_name = 'ART CENTER' and s3.passive_flag=false
);
试图让事情产生影响:
List<Predicate> predicates = new ArrayList<>();
Expression<String> sfExpression = null;
List<Predicate> siteNamePredicates = new ArrayList<>();
Expression<String> site1Expression = pathRoot.join("segments", JoinType.LEFT)
.join("segmentEnds", JoinType.LEFT)
.get(pathSearch.getPathSearchField().get().toString());
siteNamePredicates.add(cb.equal(site1Expression.in(site1Expression), "ABC"));
Expression<String> site2Expression = pathRoot.join("segments", JoinType.LEFT)
.join("segmentEnds", JoinType.LEFT)
.get(pathSearch.getPathSearchField().get().toString());
siteNamePredicates.add(cb.equal(site2Expression.in(site2Expression), "ART CENTER"));
predicates.add(cb.and(siteNamePredicates.toArray(new Predicate[siteNamePredicates.size() -1])));
return cb.and(predicates.toArray(new Predicate[predicates.size() - 1]));
使用规范的原因是我们允许我们的应用程序使用AND和其他搜索条件,因此使用CriteriaBuilder
可以使用predicates
。
任何帮助都将不胜感激。