Java Code Examples for javax.persistence.criteria.CriteriaQuery#having()
The following examples show how to use
javax.persistence.criteria.CriteriaQuery#having() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RdbmsUtils.java From modeldb with Apache License 2.0 | 6 votes |
/** * Return the data count base on the criteria query * * @param session : hibernate session * @param root : entity root which is further used for getting sub filed path from it and set in * criteria where clause. Ex: Root<ProjectEntity> projectRoot = * criteriaQuery.from(ProjectEntity.class); * @param criteria : Hibernate criteria query reference for further process * @param <T> : T = entity name like ProjectEntity, DatasetEntity, ExperimentEntity etc. * @return {@link Long} : total records count */ public static <T> long count(Session session, Root<T> root, CriteriaQuery<T> criteria) { final CriteriaBuilder builder = session.getCriteriaBuilder(); final CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class); countCriteria.select(builder.count(root)); countCriteria.getRoots().addAll(criteria.getRoots()); final Predicate whereRestriction = criteria.getRestriction(); if (whereRestriction != null) { countCriteria.where(whereRestriction); } final Predicate groupRestriction = criteria.getGroupRestriction(); if (groupRestriction != null) { countCriteria.having(groupRestriction); } countCriteria.groupBy(criteria.getGroupList()); countCriteria.distinct(criteria.isDistinct()); return session.createQuery(countCriteria).getSingleResult(); }
Example 2
Source File: JpaUtil.java From linq with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T> TypedQuery<Long> getCountQuery(CriteriaQuery<T> cq) { Class<T> domainClass = cq.getResultType(); EntityManager em = getEntityManager(domainClass); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> countCq = cb.createQuery(Long.class); Root<T> root; if (cq.getRestriction() != null) { countCq.where(cq.getRestriction()); } if (cq.getGroupRestriction() != null) { countCq.having(cq.getGroupRestriction()); } if (cq.getRoots().isEmpty()) { root = countCq.from(domainClass); } else { countCq.getRoots().addAll(cq.getRoots()); root = (Root<T>) countCq.getRoots().iterator().next(); } countCq.groupBy(cq.getGroupList()); if (cq.isDistinct()) { countCq.select(cb.countDistinct(root)); } else { countCq.select(cb.count(root)); } return em.createQuery(countCq); }
Example 3
Source File: CommandPredicates.java From genie with Apache License 2.0 | 5 votes |
/** * Get a predicate using the specified parameters. * * @param root The {@link Root} (from) for this query * @param cq The {@link CriteriaQuery} instance this predicate is for * @param cb The {@link CriteriaBuilder} for the query * @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 {@link Predicate} object used for querying */ public static Predicate find( final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb, @Nullable final String name, @Nullable final String user, @Nullable final Set<String> statuses, @Nullable final Set<TagEntity> tags ) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add( PredicateUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.name), name) ); } if (StringUtils.isNotBlank(user)) { predicates.add( PredicateUtils.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[0])); }
Example 4
Source File: JpaUtils.java From jdal with Apache License 2.0 | 5 votes |
/** * Copy criteria without selection and order. * @param from source Criteria. * @param to destination Criteria. */ private static void copyCriteriaWithoutSelectionAndOrder( CriteriaQuery<?> from, CriteriaQuery<?> to, boolean copyFetches) { if (isEclipseLink(from) && from.getRestriction() != null) { // EclipseLink adds roots from predicate paths to critera. Skip copying // roots as workaround. } else { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); if (copyFetches) copyFetches(root, dest); } } to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); Predicate predicate = from.getRestriction(); if (predicate != null) to.where(predicate); }
Example 5
Source File: ApplicationPredicates.java From genie with Apache License 2.0 | 4 votes |
/** * Get a {@link Predicate} using the specified parameters. * * @param root The {@link Root} (from) for this query * @param cq The {@link CriteriaQuery} instance this predicate is for * @param cb The {@link CriteriaBuilder} for the query * @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 */ public static Predicate find( final Root<ApplicationEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb, @Nullable final String name, @Nullable final String user, @Nullable final Set<String> statuses, @Nullable final Set<TagEntity> tags, @Nullable final String type ) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add( PredicateUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.name), name) ); } if (StringUtils.isNotBlank(user)) { predicates.add( PredicateUtils.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( PredicateUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.type), type) ); } return cb.and(predicates.toArray(new Predicate[0])); }
Example 6
Source File: ClusterPredicates.java From genie with Apache License 2.0 | 4 votes |
/** * Generate a {@link Predicate} given the parameters. * * @param root The {@link Root} of the query * @param cq The {@link CriteriaQuery} * @param cb The {@link CriteriaBuilder} * @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 {@link Predicate} representing these parameters */ public static Predicate find( final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb, @Nullable final String name, @Nullable final Set<String> statuses, @Nullable final Set<TagEntity> tags, @Nullable final Instant minUpdateTime, @Nullable final Instant maxUpdateTime ) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add( PredicateUtils.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[0])); }