List of usage examples for javax.persistence.criteria CriteriaBuilder count
Expression<Long> count(Expression<?> x);
From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java
/** * {@inheritDoc}//from www .j a va 2 s. com */ public long getStatementsCount(Map<String, Object> filters, long userId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<Statement> root = query.from(Statement.class); query.select(builder.count(root)); addFilterCriteria(userId, filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:utils.jpa.EntityResource.java
public int getAllCount(TableSearchQuery tb, List<String> fieldsSearchBy) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); cq.select(cb.count(root)); Query query = tb.createQuery(em, cb, cq, root); if (query == null) { query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy); }// w ww . j ava 2s. c om try { return ((Long) query.getSingleResult()).intValue(); } catch (NoResultException ex) { return 0; } }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
private <T extends ResourceEntity> long getTotalResults(Class<T> clazz, Subquery<Long> internalIdQuery) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> resourceQuery = cb.createQuery(Long.class); Root<T> resourceRoot = resourceQuery.from(clazz); resourceQuery.select(cb.count(resourceRoot)) .where(cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery)); Long total = em.createQuery(resourceQuery).getSingleResult(); return total; }
From source file:ru.portal.services.TableServiceImpl.java
@Override public Page<HashMap<String, String>> findAll(String tableOrViewName, Pageable pageable) { List<HashMap<String, String>> result = new ArrayList<>(); EntityType<?> type = null;/*from ww w .j a v a2 s . c om*/ Set<EntityType<?>> set = em.getEntityManagerFactory().getMetamodel().getEntities(); for (EntityType<?> entityType : set) { if (entityType.getBindableJavaType().getAnnotation(PortalTable.class) != null) { if (entityType.getBindableJavaType().getName().equals(tableOrViewName)) { type = entityType; break; } } } Long totalRows = 0L; if (type != null) { Class<?> bindableJavaType = type.getBindableJavaType(); //count CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class); countQuery.select(criteriaBuilder.count(countQuery.from(bindableJavaType))); totalRows = em.createQuery(countQuery).getSingleResult(); //select CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(bindableJavaType); Root<?> root = cq.from(bindableJavaType); // cq.select(root); if (pageable == null) { pageable = new PageRequest(0, 50); } TypedQuery<?> query = em.createQuery(cq); query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize()); query.setMaxResults(pageable.getPageSize()); List<?> all = query.getResultList(); List<String> columns = getTableOrViewMetaData(tableOrViewName); for (Object object : all) { HashMap<String, String> res = new HashMap<>(columns.size()); Class<? extends Object> clazz = object.getClass(); for (String fieldName : columns) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Object fieldValue = field.get(object); res.put(fieldName, fieldValue.toString()); //TODO cast data integer long etc } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(TableServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } } result.add(res); } } PageImpl<HashMap<String, String>> list = new PageImpl<>(result, pageable, totalRows); return list; }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}/* w w w . j av a2 s.c o m*/ */ public long getTransactionsCount(Map<String, Object> filters, long userId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<Transaction> root = query.from(Transaction.class); query.select(builder.count(root)); addFilterCriteria(userId, filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java
@Override public long findCount() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = cb.createQuery(Long.class); Root<T> customer = criteriaQuery.from(entityClass); criteriaQuery.select(cb.count(customer)); Query query = em.createQuery(criteriaQuery); return (Long) query.getSingleResult(); }
From source file:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java
@Override @Transactional(propagation = Propagation.MANDATORY, readOnly = true) public <E extends Entity<K>, K> long getCount(final Class<E> clazz) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class); final Root<E> from = criteriaQuery.from(clazz); final Expression<Long> count = builder.count(from); criteriaQuery.select(count);/*from w w w. j a va 2s. c o m*/ return this.findSingle(criteriaQuery).longValue(); }
From source file:de.egore911.persistence.selector.AbstractSelector.java
public long count() { EntityManager em = EntityManagerUtil.getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<T> from = cq.from(getEntityClass()); List<Predicate> predicates = generatePredicateList(builder, from, cq); cq.where(predicates.toArray(new Predicate[predicates.size()])); cq.select(builder.count(from)); TypedQuery<Long> q = em.createQuery(cq); return q.getSingleResult(); }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, OrderSearchParams params, Set<UUID> processingPeriodIds, Pageable pageable, boolean count, Set<UUID> availableSupplyingFacilities, Set<UUID> availableRequestingFacilities) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); Root<Order> root = query.from(Order.class); if (count) {//from ww w. j a va2s .com CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query; query = (CriteriaQuery<T>) countQuery.select(builder.count(root)); } Predicate predicate = builder.conjunction(); predicate = isEqual(SUPPLYING_FACILITY_ID, params.getSupplyingFacilityId(), root, predicate, builder); predicate = isEqual(REQUESTING_FACILITY_ID, params.getRequestingFacilityId(), root, predicate, builder); if (!(isEmpty(availableSupplyingFacilities) && isEmpty(availableRequestingFacilities))) { Predicate orPredicate = builder.disjunction(); orPredicate = isOneOfOr(SUPPLYING_FACILITY_ID, availableSupplyingFacilities, root, orPredicate, builder); orPredicate = isOneOfOr(REQUESTING_FACILITY_ID, availableRequestingFacilities, root, orPredicate, builder); predicate = builder.and(predicate, orPredicate); } predicate = isEqual(PROGRAM_ID, params.getProgramId(), root, predicate, builder); predicate = isOneOf(PROCESSING_PERIOD_ID, processingPeriodIds, root, predicate, builder); predicate = isOneOf(ORDER_STATUS, params.getStatusAsEnum(), root, predicate, builder); query.where(predicate); if (!count && pageable != null && pageable.getSort() != null) { query = addSortProperties(query, root, pageable); } return query; }
From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java
@VisibleForTesting int getNbEntriesBeforeSlicing(EntityManager entityManager, Class<E> entityClass, CriteriaBuilder criteriaBuilder, Predicate[] predicates) { CriteriaQuery<Long> counterQuery = criteriaBuilder.createQuery(Long.class); CriteriaQuery<Long> select = counterQuery.select(criteriaBuilder.count(counterQuery.from(entityClass))); if (predicates.length > 0) { select.where(predicates);//from www. ja va2 s. c o m } return entityManager.createQuery(counterQuery).getSingleResult().intValue(); }