@Override public <T> List<T> list(Pageable pageable) { if (parent != null) { applyPredicateToCriteria(sq); return parent.list(pageable); } if (pageable == null) { return list(); } else { Sort sort = pageable.getSort(); orders.addAll(QueryUtils.toOrders(sort, root, cb)); applyPredicateToCriteria(criteria); TypedQuery<?> query = em.createQuery(criteria); query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); return transform(query, false); } }
/** * Apply ordering criteria. */ private <T> void applyOrder(final UiPageRequest uiPageRequest, final Map<String, String> mapping, final CriteriaBuilder builder, final CriteriaQuery<T> query, final Root<T> root) { // Apply the sort if (uiPageRequest.getUiSort() != null) { // Need to order the result final UiSort uiSort = uiPageRequest.getUiSort(); final String ormColumn = mapping.get(uiSort.getColumn()); if (ormColumn != null) { // ORM column is validated final Sort sort = new Sort(uiSort.getDirection(), mapping.get(uiSort.getColumn())); query.orderBy(QueryUtils.toOrders(sort, root, builder)); } } }
@Override protected Query doCreateQuery(Object[] values) { String nativeQuery = getQuery(values); JpaParameters parameters = getQueryMethod().getParameters(); ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values); String sortedQueryString = QueryUtils .applySorting(nativeQuery, accessor.getSort(), QueryUtils.detectAlias(nativeQuery)); Query query = bind(createJpaQuery(sortedQueryString), values); if (parameters.hasPageableParameter()) { Pageable pageable = (Pageable) (values[parameters.getPageableIndex()]); if (pageable != null) { query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); } } return query; }
@SuppressWarnings("unchecked") /** * 分页条件查询 * @param cq 条件 * @param pageable 分页信息 * @param <T> 领域类(实体类)范型 * @return 分页结果 */ public static <T> Page<T> findAll(CriteriaQuery<T> cq, Pageable pageable) { Class<T> domainClass = cq.getResultType(); Root<T> root; if (CollectionUtils.isEmpty(cq.getRoots())) { root = cq.from(domainClass); } else { root = (Root<T>) cq.getRoots().iterator().next(); } EntityManager em = getEntityManager(domainClass); if (pageable == null) { List<T> list = findAll(cq); return new PageImpl<T>(list); } else { Sort sort = pageable.getSort(); cq.orderBy(QueryUtils.toOrders(sort, root, em.getCriteriaBuilder())); TypedQuery<T> query = em.createQuery(cq); query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); Long total = count(cq); List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T> emptyList(); return new PageImpl<T>(content, pageable, total); } }
@Override public <T> Page<T> paging(Pageable pageable) { if (parent != null) { applyPredicateToCriteria(sq); return parent.paging(pageable); } List<T> list; if (pageable == null) { list = list(); return new PageImpl<T>(list); } else { Sort sort = pageable.getSort(); if (sort != null) { orders.addAll(QueryUtils.toOrders(sort, root, cb)); } applyPredicateToCriteria(criteria); TypedQuery<?> query = em.createQuery(criteria); query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); Long total = JpaUtil.count(criteria); List<T> content = Collections.<T> emptyList(); if (total > pageable.getOffset()) { content = transform(query, false); } return new PageImpl<T>(content, pageable, total); } }
@Override protected Page<T> readPage(final TypedQuery<T> query, final Pageable pageable, final Specification<T> spec) { query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); final Long total = QueryUtils.executeCountQuery(getCountQuery(spec)); final List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T>emptyList(); if (total > maximumRecords) { return new CustomPageImpl<>(content, pageable, maximumRecords, String.format(ERROR_MESSAGE, maximumRecords)); } else { return new PageImpl<>(content, pageable, total); } }
@Nullable private <U> TypedQuery<U> buildQuery(EntityManager entityManager, CriteriaQuery<U> criteriaQuery, Root<T> root, @Nullable Integer maxResults) { if (this.hasSort()) { criteriaQuery.orderBy(QueryUtils.toOrders(this.sort, root, this.criteriaBuilder)); } return this.buildQuery(entityManager, EntityManager::createQuery, criteriaQuery, CriteriaQuery::where, root, maxResults); }
public static <T, K> void addSortClause(Optional<Sort> sort, final CriteriaBuilder criteriaBuilder, final CriteriaQuery<T> criteriaQuery, final Root<K> root) { if (sort.isPresent()) { List<javax.persistence.criteria.Order> orderList = QueryUtils.toOrders(sort.get(), root, criteriaBuilder); criteriaQuery.orderBy(orderList); } }
/** * JPQL でページング検索します。 * <p>カウント句がうまく構築されない時はPagination#ignoreTotalをtrueにして、 * 別途通常の検索でトータル件数を算出するようにして下さい。 * <p>page に設定されたソート条件は無視されるので、 qlString 構築時に明示的な設定をしてください。 * <p>args に Map を指定した時は名前付き引数として取り扱います。 ( Map のキーには文字列を指定してください ) */ @SuppressWarnings("unchecked") public <T> PagingList<T> find(final String qlString, final Pagination page, final Object... args) { long total = page.isIgnoreTotal() ? -1L : load(QueryUtils.createCountQueryFor(qlString), args); List<T> list = bindArgs(em.createQuery(qlString), page, args).getResultList(); return new PagingList<>(list, new Pagination(page, total)); }