@Override @SuppressWarnings({ "rawtypes" }) public long getTotalRowCount() { Selection<T> selection = query.getSelection(); List<Order> orderList = query.getOrderList(); try { CriteriaBuilder builder = em.getCriteriaBuilder(); Expression<Long> countExpr; Set<Root<?>> roots = query.getRoots(); if (roots.size() != 1) { throw new IllegalStateException("cannot compute totalRowCount in case of multiple query roots"); } if (!query.getGroupList().isEmpty()) { throw new IllegalStateException("cannot compute totalRowCount for grouped queries"); } // transform query to a count query Root root = roots.iterator().next(); countExpr = builder.count(root); query.multiselect(countExpr); query.orderBy(new ArrayList<Order>()); TypedQuery countQuery = em.createQuery(query); return (Long) countQuery.getSingleResult(); } finally { // transform count query back to regular query query.multiselect(selection); query.orderBy(orderList); } }
/** Use ES here, it would be more efficient. */ public void search() { // criteriaBuilder.equal() CriteriaQuery<Long> criteriaQuery = null; criteriaQuery.getParameters(); criteriaQuery.getGroupList(); criteriaQuery.getOrderList(); criteriaQuery.getGroupRestriction(); criteriaQuery.getRestriction(); criteriaQuery.getSelection(); final Predicate predicate = null; } }
/** * Copy Criteria without Selection * * @param from source Criteria * @param to destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } if (from.getGroupList() != null) to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); if (from.getRestriction() != null) to.where(from.getRestriction()); if (from.getOrderList() != null) to.orderBy(from.getOrderList()); }