List of usage examples for javax.persistence.criteria CriteriaQuery orderBy
CriteriaQuery<T> orderBy(List<Order> o);
From source file:org.finra.herd.dao.impl.HerdDaoImpl.java
/** * TODO: Remove this method once we migrate away from Oracle getStorageUploadStats that uses Oracle specific 'trunc' function. * * @param storageAlternateKey the storage alternate key * @param dateRange the date range//from w w w . j a v a 2 s . c o m * * @return the upload statistics */ private StorageDailyUploadStats getStorageUploadStatsOracle(StorageAlternateKeyDto storageAlternateKey, DateRangeDto dateRange) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage file. Root<StorageFileEntity> storageFileEntity = criteria.from(StorageFileEntity.class); // Join to the other tables we can filter on. Join<StorageFileEntity, StorageUnitEntity> storageUnitEntity = storageFileEntity .join(StorageFileEntity_.storageUnit); Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage); // Create paths. Expression<Date> truncCreatedOnDateExpression = builder.function("trunc", Date.class, storageFileEntity.get(StorageFileEntity_.createdOn)); Expression<Long> totalFilesExpression = builder.count(storageFileEntity.get(StorageFileEntity_.id)); Expression<Long> totalBytesExpression = builder .sum(storageFileEntity.get(StorageFileEntity_.fileSizeBytes)); // Create the standard restrictions (i.e. the standard where clauses). Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageAlternateKey.getStorageName().toUpperCase()); Predicate createDateRestriction = builder.and( builder.greaterThanOrEqualTo(truncCreatedOnDateExpression, dateRange.getLowerDate()), builder.lessThanOrEqualTo(truncCreatedOnDateExpression, dateRange.getUpperDate())); criteria.multiselect(truncCreatedOnDateExpression, totalFilesExpression, totalBytesExpression); criteria.where(builder.and(storageNameRestriction, createDateRestriction)); // Create the group by clause. List<Expression<?>> grouping = new ArrayList<>(); grouping.add(truncCreatedOnDateExpression); criteria.groupBy(grouping); // Create the order by clause. criteria.orderBy(builder.asc(truncCreatedOnDateExpression)); // Retrieve and return the storage upload statistics. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); StorageDailyUploadStats uploadStats = new StorageDailyUploadStats(); for (Tuple tuple : tuples) { StorageDailyUploadStat uploadStat = new StorageDailyUploadStat(); uploadStats.getStorageDailyUploadStats().add(uploadStat); uploadStat.setUploadDate( HerdDateUtils.getXMLGregorianCalendarValue(tuple.get(truncCreatedOnDateExpression))); uploadStat.setTotalFiles(tuple.get(totalFilesExpression)); uploadStat.setTotalBytes(tuple.get(totalBytesExpression)); } return uploadStats; }
From source file:org.finra.herd.dao.impl.HerdDaoImpl.java
/** * {@inheritDoc}//from w w w . j av a 2s.c o m */ @Override @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public List<String> getSecurityFunctionsForRole(String roleCd) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); // CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); CriteriaQuery<String> criteria = builder.createQuery(String.class); // The criteria root is the business object definition. Root<SecurityRoleFunctionEntity> securityRoleFunctionEntity = criteria .from(SecurityRoleFunctionEntity.class); // Join to the other tables we can filter on. Join<SecurityRoleFunctionEntity, SecurityRoleEntity> securityRoleEntity = securityRoleFunctionEntity .join(SecurityRoleFunctionEntity_.securityRole); Join<SecurityRoleFunctionEntity, SecurityFunctionEntity> securityFunctionEntity = securityRoleFunctionEntity .join(SecurityRoleFunctionEntity_.securityFunction); // Get the columns. Path<String> functionCodeColumn = securityFunctionEntity.get(SecurityFunctionEntity_.code); // Add the select clause. criteria.select(functionCodeColumn); criteria.where(builder.equal(builder.upper(securityRoleEntity.get(SecurityRoleEntity_.code)), roleCd.toUpperCase())); // Add the order by clause. criteria.orderBy(builder.asc(functionCodeColumn)); // Run the query to get a list of tuples back. return entityManager.createQuery(criteria).getResultList(); }
From source file:org.finra.herd.dao.impl.HerdDaoImpl.java
/** * {@inheritDoc}/*from www . j a va 2s . co m*/ */ @Override @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public List<String> getSecurityFunctions() { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<String> criteria = builder.createQuery(String.class); // The criteria root is the business object definition. Root<SecurityFunctionEntity> securityFunctionEntity = criteria.from(SecurityFunctionEntity.class); // Get the columns. Path<String> functionCodeColumn = securityFunctionEntity.get(SecurityFunctionEntity_.code); // Add the select clause. criteria.select(functionCodeColumn); // Add the order by clause. criteria.orderBy(builder.asc(functionCodeColumn)); // Run the query to get a list of tuples back. return entityManager.createQuery(criteria).getResultList(); }
From source file:org.finra.herd.dao.impl.StorageUnitNotificationRegistrationDaoImpl.java
@Override public List<NotificationRegistrationKey> getStorageUnitNotificationRegistrationKeysByNamespace( String namespace) {//w w w. jav a2 s . c o m // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage unit notification registration. Root<StorageUnitNotificationRegistrationEntity> businessObjectDataNotificationEntityRoot = criteria .from(StorageUnitNotificationRegistrationEntity.class); // Join to the other tables we can filter on. Join<StorageUnitNotificationRegistrationEntity, NamespaceEntity> namespaceEntityJoin = businessObjectDataNotificationEntityRoot .join(StorageUnitNotificationRegistrationEntity_.namespace); // Get the columns. Path<String> notificationRegistrationNamespaceColumn = namespaceEntityJoin.get(NamespaceEntity_.code); Path<String> notificationRegistrationNameColumn = businessObjectDataNotificationEntityRoot .get(StorageUnitNotificationRegistrationEntity_.name); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)), namespace.toUpperCase()); // Add the select clause. criteria.multiselect(notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn); // Add the where clause. criteria.where(queryRestriction); // Add the order by clause. criteria.orderBy(builder.asc(notificationRegistrationNameColumn)); // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Populate the list of keys from the returned tuples. return getNotificationRegistrationKeys(tuples, notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn); }
From source file:org.jboss.pressgang.ccms.filter.utils.JPAUtils.java
/** * Copy Criteria without Selection/*from w ww.ja v a 2 s .c o m*/ * * @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()); }
From source file:org.medici.bia.dao.volume.VolumeDAOJpaImpl.java
/** * {@inheritDoc}/*from ww w . j a va 2 s .co m*/ */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Page searchVolumes(String text, PaginationFilter paginationFilter) throws PersistenceException { // Create criteria objects CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); Page page = new Page(paginationFilter); if (paginationFilter.getTotal() == null) { CriteriaQuery<Long> criteriaQueryCount = criteriaBuilder.createQuery(Long.class); Root<Volume> rootCount = criteriaQueryCount.from(Volume.class); criteriaQueryCount.select(criteriaBuilder.count(rootCount)); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add( criteriaBuilder.like((Expression) rootCount.get("serieList").get("title"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("serieList").get("subTitle1"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("serieList").get("subTitle2"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("orgNotes"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("recips"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("researcher"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) rootCount.get("senders"), "%" + text + "%")); //If we omiss criteriaBuilder.or every predicate is in conjunction with others criteriaQueryCount.where(criteriaBuilder.or(predicates.toArray(new Predicate[] {}))); TypedQuery typedQueryCount = getEntityManager().createQuery(criteriaQueryCount); page.setTotal(new Long((Long) typedQueryCount.getSingleResult())); } CriteriaQuery<Volume> criteriaQuery = criteriaBuilder.createQuery(Volume.class); Root<Volume> root = criteriaQuery.from(Volume.class); //We need to duplicate predicates beacause they are link to Root element List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(criteriaBuilder.like((Expression) root.get("serieList").get("title"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("serieList").get("subTitle1"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("serieList").get("subTitle2"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("orgNotes"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("recips"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("researcher"), "%" + text + "%")); predicates.add(criteriaBuilder.like((Expression) root.get("senders"), "%" + text + "%")); //If we omiss criteriaBuilder.or every predicate is in conjunction with others criteriaQuery.where(criteriaBuilder.or(predicates.toArray(new Predicate[] {}))); criteriaQuery.orderBy(criteriaBuilder.asc(root.get("summaryId"))); // Set values in predicate's elements TypedQuery<Volume> typedQuery = getEntityManager().createQuery(criteriaQuery); typedQuery.setFirstResult(paginationFilter.getFirstRecord()); typedQuery.setMaxResults(paginationFilter.getLength()); page.setList(typedQuery.getResultList()); return page; }
From source file:org.patientview.repository.impl.ResultHeadingDaoImpl.java
@Override public List<ResultHeading> get(int panel, Specialty specialty) { CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<ResultHeading> criteria = builder.createQuery(ResultHeading.class); Root<ResultHeading> root = criteria.from(ResultHeading.class); List<Predicate> wherePredicates = new ArrayList<Predicate>(); wherePredicates.add(builder.equal(root.get(ResultHeading_.panel), panel)); wherePredicates.add(builder.equal(root.get(ResultHeading_.specialty), specialty)); buildWhereClause(criteria, wherePredicates); criteria.orderBy(builder.asc(root.get(ResultHeading_.panelorder))); return getEntityManager().createQuery(criteria).getResultList(); }
From source file:org.reusables.access.AbstractEntityManagerRepository.java
@Override public List<T> findAll(final String orderByPropertyName) { final CriteriaBuilder bld = getEm().getCriteriaBuilder(); final CriteriaQuery<T> query = bld.createQuery(this.entityClass); final Root<T> root = query.from(this.entityClass); if (orderByPropertyName != null) { query.orderBy(bld.asc(root.get(orderByPropertyName))); }//from w w w.ja v a2 s . c o m return getEm().createQuery(query).getResultList(); }
From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java
protected void attachOrderBy(ProductSearchCriteria searchCriteria, From<?, ? extends Product> product, Path<? extends Sku> sku, CriteriaQuery<?> criteria) { if (StringUtils.isNotBlank(searchCriteria.getSortQuery())) { CriteriaBuilder builder = em.getCriteriaBuilder(); List<Order> sorts = new ArrayList<Order>(); String sortQueries = searchCriteria.getSortQuery(); for (String sortQuery : sortQueries.split(",")) { String[] sort = sortQuery.split(" "); if (sort.length == 2) { String key = sort[0]; boolean asc = sort[1].toLowerCase().contains("asc"); // Determine whether we should use the product path or the sku path Path<?> pathToUse; if (key.contains("defaultSku.")) { pathToUse = sku;// www. j a v a 2 s . co m key = key.substring("defaultSku.".length()); } else if (key.contains("product.")) { pathToUse = product; key = key.substring("product.".length()); } else { // We don't know which path this facet is built on - resolves previous bug that attempted // to attach search facet to any query parameter continue; } if (asc) { sorts.add(builder.asc(pathToUse.get(key))); } else { sorts.add(builder.desc(pathToUse.get(key))); } } } criteria.orderBy(sorts.toArray(new Order[sorts.size()])); } }
From source file:org.springframework.data.jpa.repository.support.SimpleJpaRepository.java
/** * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}. * //from w w w . ja va 2 s .com * @param spec can be {@literal null}. * @param sort can be {@literal null}. * @return */ protected TypedQuery<T> getQuery(Specification<T> spec, Sort sort) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery(getDomainClass()); Root<T> root = applySpecificationToCriteria(spec, query); query.select(root); if (sort != null) { query.orderBy(toOrders(sort, root, builder)); } return applyRepositoryMethodMetadata(em.createQuery(query)); }