List of usage examples for javax.persistence.criteria CriteriaBuilder count
Expression<Long> count(Expression<?> x);
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query that performs a count * /*from w ww . jav a 2 s . c o m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param filter * the filter to apply * @param distinct * whether to return only distinct results * @return */ public static <T> CriteriaQuery<Long> createCountQuery(EntityManager entityManager, Class<T> entityClass, Filter filter, boolean distinct) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<T> root = cq.from(entityClass); cq.select(distinct ? builder.countDistinct(root) : builder.count(root)); Predicate p = createPredicate(filter, builder, root); if (p != null) { cq.where(p); } return cq; }
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Create a row count CriteriaQuery from a CriteriaQuery * @param em entity manager/*from w w w. jav a 2s .c o m*/ * @param criteria source criteria * @return row count CriteriaQuery */ public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class); copyCriteriaWithoutSelectionAndOrder(criteria, countCriteria); Expression<Long> countExpression; if (criteria.isDistinct()) { countExpression = builder.countDistinct(findRoot(countCriteria, criteria.getResultType())); } else { countExpression = builder.count(findRoot(countCriteria, criteria.getResultType())); } return countCriteria.select(countExpression); }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaClusterSpecs.java
/** * Generate a specification given the parameters. * * @param name The name of the cluster to find * @param statuses The statuses of the clusters to find * @param tags The tags of the clusters to find * @param minUpdateTime The minimum updated time of the clusters to find * @param maxUpdateTime The maximum updated time of the clusters to find * @return The specification//from ww w .j av a 2s.co m */ public static Specification<ClusterEntity> find(@Nullable final String name, @Nullable final Set<ClusterStatus> statuses, @Nullable final Set<TagEntity> tags, @Nullable final Instant minUpdateTime, @Nullable final Instant maxUpdateTime) { return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ClusterEntity_.name), name)); } if (minUpdateTime != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(ClusterEntity_.updated), minUpdateTime)); } if (maxUpdateTime != null) { predicates.add(cb.lessThan(root.get(ClusterEntity_.updated), maxUpdateTime)); } if (tags != null && !tags.isEmpty()) { final Join<ClusterEntity, TagEntity> tagEntityJoin = root.join(ClusterEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(ClusterEntity_.id)); cq.having(cb.equal(cb.count(root.get(ClusterEntity_.id)), tags.size())); } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(ClusterEntity_.status), status)) .toArray(Predicate[]::new))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:org.apache.cxf.fediz.service.idp.service.jpa.DBInitApplicationListener.java
protected boolean isDBEmpty() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); cq.select(cb.count(cq.from(ClaimEntity.class))); return em.createQuery(cq).getSingleResult() == 0; }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaCommandSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the command * @param user The name of the user who created the command * @param statuses The status of the command * @param tags The set of tags to search the command for * @return A specification object used for querying *//* www.j ava 2 s. c o m*/ public static Specification<CommandEntity> find(@Nullable final String name, @Nullable final String user, @Nullable final Set<CommandStatus> statuses, @Nullable final Set<TagEntity> tags) { return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status)) .toArray(Predicate[]::new))); } if (tags != null && !tags.isEmpty()) { final Join<CommandEntity, TagEntity> tagEntityJoin = root.join(CommandEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(CommandEntity_.id)); cq.having(cb.equal(cb.count(root.get(CommandEntity_.id)), tags.size())); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaApplicationSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the application * @param user The name of the user who created the application * @param statuses The status of the application * @param tags The set of tags to search with * @param type The type of applications to fine * @return A specification object used for querying */// w w w. j a v a 2 s. co m public static Specification<ApplicationEntity> find(@Nullable final String name, @Nullable final String user, @Nullable final Set<ApplicationStatus> statuses, @Nullable final Set<TagEntity> tags, @Nullable final String type) { return (final Root<ApplicationEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(ApplicationEntity_.status), status)) .toArray(Predicate[]::new))); } if (tags != null && !tags.isEmpty()) { final Join<ApplicationEntity, TagEntity> tagEntityJoin = root.join(ApplicationEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(ApplicationEntity_.id)); cq.having(cb.equal(cb.count(root.get(ApplicationEntity_.id)), tags.size())); } if (StringUtils.isNotBlank(type)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.type), type)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.boxedfolder.carrot.repository.impl.AnalyticsLogRepositoryImpl.java
private TypedQuery<Long> getCountQuery(Class clazz) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); query.select(builder.count(query.from(clazz))); return entityManager.createQuery(query); }
From source file:com.nuevebit.persistence.repository.JPASearchableRepository.java
@Override public long count(S searchCriteria) { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> q = cb.createQuery(Long.class); Root<T> p = createQueryRoot(q, searchCriteria); q.select(cb.count(p)); return getEntityManager().createQuery(q).getSingleResult(); }
From source file:net.przemkovv.sphinx.dao.impl.DefaultUserDAO.java
@Override public boolean existsByEmail(String email) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<User> user = cq.from(User.class); cq.where(cb.equal(user.get(User_.email), email)); cq.select(cb.count(user)); return em.createQuery(cq).getSingleResult().intValue() == 1; }
From source file:net.przemkovv.sphinx.dao.impl.DefaultGenericDAO.java
@Override public Long countAll() { CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); cq.select(cb.count(cq.from(type))); return getEntityManager().createQuery(cq).getSingleResult(); }