我有 JpaAdvertiseCacheRepositoryImpl,它是 SpringDataJpa 的自定义存储库实现。我需要这个来满足复杂的标准。
我设法通过 Predicates 过滤条件并通过 TypedQuery 进行分页。
我现在的大问题是排序!在 repo 的自定义实现中找不到执行此操作的方法。
有人知道该怎么做吗?
肿瘤坏死因子
public class JpaAdvertiseCacheRepositoryImpl implements JpaAdvertiseCacheRepositoryCustom {
@PersistenceContext
private EntityManager em;
@Override
public List<AdvertiseCacheJpa> findByFilter(FilterBuilder filter) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<AdvertiseCacheJpa> query = builder.createQuery(AdvertiseCacheJpa.class);
Root<AdvertiseCacheJpa> from = query.from(AdvertiseCacheJpa.class);
//apply filter <- applyFilter returns array of predicates
Predicate[] predicates = applyFilter(filter, builder, from);
if (predicates.length > 0) {
//query.where(builder.and(predicates));
query.where(predicates);
}
//apply sorting <- THIS HAVE TO WORK SOON :)
Sort sort = new Sort(Sort.Direction.ASC, "price");
//query.orderBy()
//apply pagination
TypedQuery typedQuery = em.createQuery(query);
typedQuery = typedQuery.setFirstResult(filter.getOffset());
typedQuery = typedQuery.setMaxResults(filter.getMaxItemsPerPage());
return typedQuery.getResultList();
//return em.createQuery(query).getResultList();
}