我从Collection<String> sportCodes
表中收集了codes
的sport
。
我可以建立一个谓词......
private Collection<String> sportCodes;
private Predicate searchBySportsPredicate(Root<PlaygroundEntity> root) {
return root.join(PlaygroundEntity_.specializations).get(SportEntity_.code).in(sportCodes);
}
...并获得PlaygroundEntity
与sport codes
系列中包含的一组sportCodes
。
我怎么能这样做呢?
即建立这样的谓词......
(sportCodes).in(root.join(PlaygroundEntity_.specializations).get(SportEntity_.code))
...并获得PlaygroundEntity
与一套sport codes
,其中包括sportCodes
系列。
我很感激任何帮助。
我在这个问题上花了3天时间,解决方案太简单了:
private Predicate searchBySportsPredicate(Root<PlaygroundEntity> root, CriteriaBuilder cb) {
Collection<Predicate> predicates = new ArrayList<>();
for (String sportCode : sportCodes) {
predicates.add(
cb.equal(root.join(PlaygroundEntity_.specializations).get(SportEntity_.code), sportCode)
);
}
return cb.and(predicates.toArray(new Predicate[0]));
}
重要
它不起作用:
private Predicate searchBySportsPredicate(Root<PlaygroundEntity> root, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>(sportCodes.size());
Path<String> codePath = root.join(PlaygroundEntity_.specializations).get(SportEntity_.code)
for (String sportCode : sportCodes) {
predicates.add(cb.equal(codePath, sportCode));
}
return cb.and(predicates.toArray(new Predicate[0]));
}
希望它可以帮助某人。