我被困在一个查询中,我正在使用 Spring Data JPA、JpaRepository 和 JpaSpecificationExecutor。所以我必须使用Predicate
s from CriteriaBuilder
。
目前我只是这样做:
Specifications<MyEntity> spec = Specifications.where(MySpec.isTrue());
List<MyEntity> myEntities = myRepository.findAll(spec);
其中 MySpec.isTrue() 是:
public static Specification<MyEntity> isTrue() {
return new Specification<MyEntity>() {
@Override
public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
SetJoin<MyEntity, JoinEntity> j = root.join(MyEntity.joinEntities, JoinType.LEFT);
return cb.isTrue(j.get(JoinEntity_.attr));
}
};
}
所以这当然会导致这个 SQL:
SELECT e.* FROM MyEntity e
LEFT OUTER JOIN JoinEntity j ON j.myEntityId = e.id
WHERE j.attr = true
但我只对独特的MyEntity
s 集感兴趣。所以在 JPQL 我会写:
SELECT DISTINCT(e) FROM MyEntity e
LEFT JOIN e.joinEntities j
WHERE j.attr = true
现在我的解决方案是:
List<MyEntity> myEntities = myRepository.findAll(spec);
Set<MyEntity> entitiesSet = new HashSet<MyEntity>(myEntities);
一定有更好的方法;)
它如何与CriteriaBuilder
(和JpaSpecificationExecutor
)一起使用?
解决方案:
第一个想法很简单:
public static Specification<MyEntity> isTrue() {
return new Specification<MyEntity>() {
@Override
public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
SetJoin<MyEntity, JoinEntity> j = root.join(MyEntity.joinEntities, JoinType.LEFT);
query.distinct(true); // <<-- HERE
return cb.isTrue(j.get(JoinEntity_.attr));
}
};
}
这行得通,但有点破坏了那些小规格部件的感觉。所以我想出了一个使用子查询的解决方案。这可能需要一些额外的时间,但对我来说这并不重要:
public static Specification<MyEntity> isTrue() {
return new Specification<MyEntity>() {
@Override
public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Subquery<JoinEntity> subquery = query.subquery(JoinEntity.class);
Root<JoinEntity> subRoot = subquery.from(JoinEntity.class);
subquery.select(subRoot);
subquery.where(cb.isTrue(subRoot.get(JoinEntity_.attr)));
subquery.groupBy(subRoot.get(JoinEntity_.myEntity));
return cb.exists(subquery);
}
};
}