如何使用Hibernate(使用Specification接口/ JpaSpecificationExecutor)创建此查询?
SELECT i1.*
FROM Images AS i1 LEFT JOIN images AS i2
ON (i1.Gallery = i2.Gallery AND i1.Submitted < i2.Submitted)
WHERE i2.Submitted IS NULL;
来源: https://www.exchangecore.com/blog/mysql-sql-retrieve-most-recent-or-largest-record-group/
到目前为止,我已经尝试过:
public Predicate toPredicate(final Root<GalleryEntity> root, final CriteriaQuery<?> query, final CriteriaBuilder criteriaBuilder) {
final List<Predicate> predicates = new ArrayList<Predicate>();
final Join join = root.join("Images", JoinType.LEFT);
join.on(criteriaBuilder.equal(root.get("Gallery"), join.get("Gallery")));
predicates.add(join.getOn());
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}