Java Code Examples for javax.persistence.criteria.CriteriaBuilder#asc()
The following examples show how to use
javax.persistence.criteria.CriteriaBuilder#asc() .
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: CriteriaBuilderTools.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 根据条件组织一个排序的语句 * @param <T> * @param <T_> * @param cb * @param root * @param cls_ * @param fieldName * @param orderType * @return */ public static <T extends JpaObject, T_ extends SliceJpaObject_>Order getOrder( CriteriaBuilder cb, Root<T> root, Class<T_> cls_, String fieldName, String orderType ) { Boolean fieldExists = false; Field[] fields = cls_.getDeclaredFields(); for( Field field : fields ) { if( field.getName().equalsIgnoreCase( fieldName ) ) { fieldName = field.getName(); //校正排序列的名称 fieldExists = true; } } if( !fieldExists ) { //如果排序列不存在,就直接返回空,不排序,让SQL可以正常执行 return null; } if( "desc".equalsIgnoreCase( orderType )) { return cb.desc( root.get( fieldName ).as(String.class) ); }else { return cb.asc( root.get( fieldName ).as(String.class) ); } }
Example 2
Source File: UserNamespaceAuthorizationDaoImpl.java From herd with Apache License 2.0 | 6 votes |
@Override public List<UserNamespaceAuthorizationEntity> getUserNamespaceAuthorizationsByNamespace(String namespace) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<UserNamespaceAuthorizationEntity> criteria = builder.createQuery(UserNamespaceAuthorizationEntity.class); // The criteria root is the user namespace authorization. Root<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntity = criteria.from(UserNamespaceAuthorizationEntity.class); // Join to the other tables we can filter on. Join<UserNamespaceAuthorizationEntity, NamespaceEntity> namespaceEntity = userNamespaceAuthorizationEntity.join(UserNamespaceAuthorizationEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), namespace.toUpperCase()); // Order by user id. Order orderBy = builder.asc(userNamespaceAuthorizationEntity.get(UserNamespaceAuthorizationEntity_.userId)); // Add all clauses for the query. criteria.select(userNamespaceAuthorizationEntity).where(queryRestriction).orderBy(orderBy); // Execute the query and return the result list. return entityManager.createQuery(criteria).getResultList(); }
Example 3
Source File: BusinessObjectFormatDaoImpl.java From herd with Apache License 2.0 | 6 votes |
@Override public List<Long> getBusinessObjectFormatIdsByBusinessObjectDefinition(BusinessObjectDefinitionEntity businessObjectDefinitionEntity) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); // The criteria root is the business object format. Root<BusinessObjectFormatEntity> businessObjectFormatEntityRoot = criteria.from(BusinessObjectFormatEntity.class); // Create path. Expression<Long> businessObjectFormatIdColumn = businessObjectFormatEntityRoot.get(BusinessObjectFormatEntity_.id); // Create standard restrictions. Predicate predicate = builder.equal(businessObjectFormatEntityRoot.get(BusinessObjectFormatEntity_.businessObjectDefinitionId), businessObjectDefinitionEntity.getId()); // Build an order by clause. Order orderBy = builder.asc(businessObjectFormatEntityRoot.get(BusinessObjectFormatEntity_.id)); // Add all clauses to the query. criteria.select(businessObjectFormatIdColumn).where(predicate).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).getResultList(); }
Example 4
Source File: ActionListPagingWithGroup.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private List<PersonCard> list(EffectivePerson effectivePerson, Business business, Integer adjustPage, Integer adjustPageSize, String grouptype, String keyString) throws Exception { EntityManager em = business.entityManagerContainer().get(PersonCard.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<PersonCard> cq = cb.createQuery(PersonCard.class); Root<PersonCard> root = cq.from(PersonCard.class); Order _order = cb.asc(root.get(PersonCard_.orderNumber)); Predicate pe = cb.equal(root.get(PersonCard_.distinguishedName), effectivePerson.getDistinguishedName()); if(StringUtils.isNotEmpty(grouptype)){ pe = cb.and(pe,cb.equal(root.get(PersonCard_.groupType),grouptype)); } if (StringUtils.isNotEmpty(keyString)) { String key = StringUtils.trim(StringUtils.replaceEach(keyString, new String[] { "\u3000", "?", "%" }, new String[] { " ", "", "" })); if (StringUtils.isNotEmpty(key)) { Predicate p = cb.or(cb.like(root.get(PersonCard_.name), "%" + key + "%"), cb.like(root.get(PersonCard_.status), "%" + key + "%"), cb.like(root.get(PersonCard_.mobile), "%" + key + "%"), cb.like(root.get(PersonCard_.officePhone), "%" + key + "%"), cb.like(root.get(PersonCard_.pinyin), "%" + key + "%"), cb.like(root.get(PersonCard_.pinyinInitial), "%" + key + "%")); p = cb.and(p,pe); cq.select(root).where(p).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } else { cq.select(root).where(pe).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } } else { cq.select(root).where(pe).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } return em.createQuery(cq).setFirstResult((adjustPage - 1) * adjustPageSize).setMaxResults(adjustPageSize).getResultList(); }
Example 5
Source File: StorageFileDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<String> getStorageFilesByStorageAndFilePathPrefix(String storageName, String filePathPrefix) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageFileEntity> criteria = builder.createQuery(StorageFileEntity.class); // The criteria root is the storage files. 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 the standard restrictions (i.e. the standard where clauses). Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path), String.format("%s%%", filePathPrefix)); Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase()); // Order the results by file path. Order orderByFilePath = builder.asc(storageFileEntity.get(StorageFileEntity_.path)); criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageNameRestriction)).orderBy(orderByFilePath); // Retrieve the storage files. List<StorageFileEntity> storageFileEntities = entityManager.createQuery(criteria).getResultList(); // Build the result list. List<String> storageFilePaths = new ArrayList<>(); for (StorageFileEntity storageFile : storageFileEntities) { storageFilePaths.add(storageFile.getPath()); } return storageFilePaths; }
Example 6
Source File: AllowedAttributeValueDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<AllowedAttributeValueEntity> getAllowedAttributeValuesByAttributeValueListKey(AttributeValueListKey attributeValueListKey) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<AllowedAttributeValueEntity> criteria = builder.createQuery(AllowedAttributeValueEntity.class); // The criteria root is the allowed attribute value. Root<AllowedAttributeValueEntity> allowedAttributeValueEntityRoot = criteria.from(AllowedAttributeValueEntity.class); // Join to the other tables we can filter on. Join<AllowedAttributeValueEntity, AttributeValueListEntity> attributeValueListEntityJoin = allowedAttributeValueEntityRoot.join(AllowedAttributeValueEntity_.attributeValueList); Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityJoin.join(AttributeValueListEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(builder.upper(attributeValueListEntityJoin.get(AttributeValueListEntity_.name)), attributeValueListKey.getAttributeValueListName().toUpperCase())); predicates.add(builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)), attributeValueListKey.getNamespace().toUpperCase())); // Order the results by allowed attribute value. Order orderByAllowedAttributeValue = builder.asc(allowedAttributeValueEntityRoot.get(AllowedAttributeValueEntity_.allowedAttributeValue)); // Add the clauses for the query. criteria.select(allowedAttributeValueEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))) .orderBy(orderByAllowedAttributeValue); // Execute the query and return the results. return entityManager.createQuery(criteria).getResultList(); }
Example 7
Source File: JpaDao.java From jdal with Apache License 2.0 | 5 votes |
/** * Get JPA Order from a page order for a CriteriaQuery * @param page request page * @param criteria CriteriaQuery to apply Order on. * @return new Order */ private Order getOrder(Page<?> page, CriteriaQuery<?> criteria) { CriteriaBuilder cb = em.getCriteriaBuilder(); Root<T> root = JpaUtils.findRoot(criteria, getEntityClass()); String propertyPath = page.getSortName(); if (log.isDebugEnabled()) log.debug("Setting order as: " + propertyPath); Path<?> path = JpaUtils.getPath(root, propertyPath); return page.getOrder() == Page.Order.ASC ? cb.asc(path) : cb.desc(path); }
Example 8
Source File: StorageUnitDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<StorageUnitEntity> getS3StorageUnitsToRestore(int maxResult) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Join to the other tables we can filter on. Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.storage); Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform); Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.status); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storagePlatformEntityJoin.get(StoragePlatformEntity_.name), StoragePlatformEntity.S3)); predicates.add(builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.RESTORING)); // Order the results by storage unit updated on timestamp. Order orderBy = builder.asc(storageUnitEntityRoot.get(StorageUnitEntity_.updatedOn)); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).setMaxResults(maxResult).getResultList(); }
Example 9
Source File: StorageUnitDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<StorageUnitEntity> getS3StorageUnitsToExpire(int maxResult) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Join to the other tables we can filter on. Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.storage); Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform); Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.status); // Get the current time. Timestamp currentTime = new Timestamp(System.currentTimeMillis()); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storagePlatformEntityJoin.get(StoragePlatformEntity_.name), StoragePlatformEntity.S3)); predicates.add(builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.RESTORED)); predicates.add(builder.lessThan(storageUnitEntityRoot.get(StorageUnitEntity_.restoreExpirationOn), currentTime)); // Order the results. Order orderBy = builder.asc(storageUnitEntityRoot.get(StorageUnitEntity_.restoreExpirationOn)); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).setMaxResults(maxResult).getResultList(); }
Example 10
Source File: StorageUnitDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<StorageUnitEntity> getLatestVersionStorageUnitsByStoragePlatformAndFileType(String storagePlatform, String businessObjectFormatFileType) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Join to the other tables we can filter on. Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.storage); Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform); Join<StorageUnitEntity, BusinessObjectDataEntity> businessObjectDataEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.businessObjectData); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityJoin.join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.fileType); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(builder.upper(storagePlatformEntityJoin.get(StoragePlatformEntity_.name)), storagePlatform.toUpperCase())); predicates.add(builder.equal(builder.upper(fileTypeEntityJoin.get(FileTypeEntity_.code)), businessObjectFormatFileType.toUpperCase())); predicates.add(builder.isTrue(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.latestVersion))); predicates.add(builder.isTrue(businessObjectDataEntityJoin.get(BusinessObjectDataEntity_.latestVersion))); // Order by storage unit created on timestamp. Order orderBy = builder.asc(storageUnitEntityRoot.get(StorageUnitEntity_.createdOn)); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).getResultList(); }
Example 11
Source File: ActionListPaging.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private List<PersonCard> list(EffectivePerson effectivePerson, Business business, Integer adjustPage, Integer adjustPageSize, String keyString) throws Exception { EntityManager em = business.entityManagerContainer().get(PersonCard.class); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<PersonCard> cq = cb.createQuery(PersonCard.class); Root<PersonCard> root = cq.from(PersonCard.class); Order _order = cb.asc(root.get(PersonCard_.orderNumber)); Predicate pe = cb.equal(root.get(PersonCard_.distinguishedName), effectivePerson.getDistinguishedName()); if (StringUtils.isNotEmpty(keyString)) { String key = StringUtils.trim(StringUtils.replaceEach(keyString, new String[] { "\u3000", "?", "%" }, new String[] { " ", "", "" })); if (StringUtils.isNotEmpty(key)) { Predicate p = cb.or(cb.like(root.get(PersonCard_.name), "%" + key + "%"), cb.like(root.get(PersonCard_.status), "%" + key + "%"), cb.like(root.get(PersonCard_.mobile), "%" + key + "%"), cb.like(root.get(PersonCard_.officePhone), "%" + key + "%"), cb.like(root.get(PersonCard_.pinyin), "%" + key + "%"), cb.like(root.get(PersonCard_.pinyinInitial), "%" + key + "%"), cb.like(root.get(PersonCard_.groupType), "%" + key + "%")); p = cb.and(p,pe); cq.select(root).where(p).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } else { cq.select(root).where(pe).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } } else { cq.select(root).where(pe).orderBy(cb.asc(root.get(PersonCard_.orderNumber))); } return em.createQuery(cq).setFirstResult((adjustPage - 1) * adjustPageSize).setMaxResults(adjustPageSize).getResultList(); }
Example 12
Source File: BusinessObjectDataDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<BusinessObjectDataEntity> getBusinessObjectDataFromStorageOlderThan(StorageEntity storageEntity, int thresholdMinutes, List<String> businessObjectDataStatuses) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class); // The criteria root is the business object data. Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.storageUnits); // Compute threshold timestamp based on the current database timestamp and threshold minutes. Timestamp thresholdTimestamp = HerdDateUtils.addMinutes(getCurrentTimestamp(), -thresholdMinutes); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storageUnitEntityJoin.get(StorageUnitEntity_.storageName), storageEntity.getName())); predicates.add(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.statusCode).in(businessObjectDataStatuses)); predicates.add(builder.lessThanOrEqualTo(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.createdOn), thresholdTimestamp)); // Order results by "created on" timestamp. Order orderBy = builder.asc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.createdOn)); // Add all clauses to the query. criteria.select(businessObjectDataEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).getResultList(); }
Example 13
Source File: UserNamespaceAuthorizationDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<UserNamespaceAuthorizationEntity> getUserNamespaceAuthorizationsByUserId(String userId) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<UserNamespaceAuthorizationEntity> criteria = builder.createQuery(UserNamespaceAuthorizationEntity.class); // The criteria root is the user namespace authorization. Root<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntity = criteria.from(UserNamespaceAuthorizationEntity.class); // Join to the other tables we can filter on. Join<UserNamespaceAuthorizationEntity, NamespaceEntity> namespaceEntity = userNamespaceAuthorizationEntity.join(UserNamespaceAuthorizationEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(userNamespaceAuthorizationEntity.get(UserNamespaceAuthorizationEntity_.userId)), userId.toUpperCase()); // Order by namespace code. Order orderBy = builder.asc(namespaceEntity.get(NamespaceEntity_.code)); // Add all clauses for the query. criteria.select(userNamespaceAuthorizationEntity).where(queryRestriction).orderBy(orderBy); // Execute the query and return the result list. return entityManager.createQuery(criteria).getResultList(); }
Example 14
Source File: UserNamespaceAuthorizationDaoImpl.java From herd with Apache License 2.0 | 5 votes |
@Override public List<String> getUserIdsWithWriteOrWriteDescriptiveContentPermissionsByNamespace(NamespaceEntity namespaceEntity) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<String> criteria = builder.createQuery(String.class); // The criteria root is the user namespace authorization. Root<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntityRoot = criteria.from(UserNamespaceAuthorizationEntity.class); // Get the user id column. Path<String> userIdColumn = userNamespaceAuthorizationEntityRoot.get(UserNamespaceAuthorizationEntity_.userId); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(userNamespaceAuthorizationEntityRoot.get(UserNamespaceAuthorizationEntity_.namespace), namespaceEntity)); predicates.add(builder.or(builder.isTrue(userNamespaceAuthorizationEntityRoot.get(UserNamespaceAuthorizationEntity_.writePermission)), builder.isTrue(userNamespaceAuthorizationEntityRoot.get(UserNamespaceAuthorizationEntity_.writeDescriptiveContentPermission)))); // Order by user id. Order orderBy = builder.asc(userNamespaceAuthorizationEntityRoot.get(UserNamespaceAuthorizationEntity_.userId)); // Add all clauses to the query. criteria.select(userIdColumn).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the result list. return entityManager.createQuery(criteria).getResultList(); }
Example 15
Source File: JPACriteriaQueryVisitor.java From cxf with Apache License 2.0 | 5 votes |
public CriteriaQuery<E> orderBy(List<SingularAttribute<T, ?>> attributes, boolean asc) { CriteriaBuilder cb = getCriteriaBuilder(); List<Order> orders = new ArrayList<>(); for (SingularAttribute<T, ?> attribute : attributes) { Path<?> selection = getRoot().get(attribute); Order order = asc ? cb.asc(selection) : cb.desc(selection); orders.add(order); } return getCriteriaQuery().orderBy(orders); }
Example 16
Source File: ResourceDao.java From osiam with MIT License | 4 votes |
public <T extends ResourceEntity> SearchResult<T> search(Class<T> clazz, ParseTree filterTree, int count, int startIndex, String sortBy, String sortOrder, FilterParser<T> filterParser) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> resourceQuery = cb.createQuery(clazz); Root<T> resourceRoot = resourceQuery.from(clazz); Subquery<Long> internalIdQuery = resourceQuery.subquery(Long.class); Root<T> internalIdRoot = internalIdQuery.from(clazz); internalIdQuery.select(internalIdRoot.get(ResourceEntity_.internalId)); if (filterTree != null && filterTree.getChildCount() > 0) { Predicate predicate = filterParser.createPredicateAndJoin(filterTree, internalIdRoot); internalIdQuery.where(predicate); } resourceQuery.select(resourceRoot).where( cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery)); // TODO: evaluate if a User-/GroupDao supplied default sortBy field is possible Expression<?> sortByField = resourceRoot.get(ResourceEntity_.id); if (sortBy != null && !sortBy.isEmpty()) { sortByField = filterParser.createSortByField(sortBy, resourceRoot); } // default order is ascending Order order = cb.asc(sortByField); if (sortOrder.equalsIgnoreCase("descending")) { order = cb.desc(sortByField); } resourceQuery.orderBy(order); TypedQuery<T> query = em.createQuery(resourceQuery); query.setFirstResult(startIndex); query.setMaxResults(count); List<T> results = query.getResultList(); long totalResult = getTotalResults(clazz, internalIdQuery); return new SearchResult<>(results, totalResult); }
Example 17
Source File: JpaWidgetRepository.java From attic-rave with Apache License 2.0 | 4 votes |
private Order getOrderByTitleAsc(CriteriaBuilder cb, Root<JpaWidget> widgetType) { return cb.asc(getTitleField(widgetType)); }
Example 18
Source File: ExpectedPartitionValueDaoImpl.java From herd with Apache License 2.0 | 4 votes |
@Override public ExpectedPartitionValueEntity getExpectedPartitionValue(ExpectedPartitionValueKey expectedPartitionValueKey, int offset) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<ExpectedPartitionValueEntity> criteria = builder.createQuery(ExpectedPartitionValueEntity.class); // The criteria root is the expected partition value. Root<ExpectedPartitionValueEntity> expectedPartitionValueEntity = criteria.from(ExpectedPartitionValueEntity.class); // Join to the other tables we can filter on. Join<ExpectedPartitionValueEntity, PartitionKeyGroupEntity> partitionKeyGroupEntity = expectedPartitionValueEntity.join(ExpectedPartitionValueEntity_.partitionKeyGroup); // Add a restriction to filter case insensitive groups that match the user specified group. Predicate whereRestriction = builder.equal(builder.upper(partitionKeyGroupEntity.get(PartitionKeyGroupEntity_.partitionKeyGroupName)), expectedPartitionValueKey.getPartitionKeyGroupName().toUpperCase()); // Depending on the offset, we might need to order the records in the query. Order orderByExpectedPartitionValue = null; // Add additional restrictions to handle expected partition value and an optional offset. if (offset == 0) { // Since there is no offset, we need to match the expected partition value exactly. whereRestriction = builder.and(whereRestriction, builder .equal(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue())); } else if (offset > 0) { // For a positive offset value, add a restriction to filter expected partition values that are >= the user specified expected partition value. whereRestriction = builder.and(whereRestriction, builder .greaterThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue())); // Order by expected partition value in ascending order. orderByExpectedPartitionValue = builder.asc(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue)); } else { // For a negative offset value, add a restriction to filter expected partition values that are <= the user specified expected partition value. whereRestriction = builder.and(whereRestriction, builder .lessThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue())); // Order by expected partition value in descending order. orderByExpectedPartitionValue = builder.desc(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue)); } // Add the clauses for the query and execute the query. if (offset == 0) { criteria.select(expectedPartitionValueEntity).where(whereRestriction); return executeSingleResultQuery(criteria, String .format("Found more than one expected partition value with parameters {partitionKeyGroupName=\"%s\", expectedPartitionValue=\"%s\"}.", expectedPartitionValueKey.getPartitionKeyGroupName(), expectedPartitionValueKey.getExpectedPartitionValue())); } else { criteria.select(expectedPartitionValueEntity).where(whereRestriction).orderBy(orderByExpectedPartitionValue); List<ExpectedPartitionValueEntity> resultList = entityManager.createQuery(criteria).setFirstResult(Math.abs(offset)).setMaxResults(1).getResultList(); return resultList.size() > 0 ? resultList.get(0) : null; } }
Example 19
Source File: StorageUnitDaoImpl.java From herd with Apache License 2.0 | 4 votes |
@Override public List<StorageUnitEntity> getS3StorageUnitsToCleanup(int maxResult) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Join to the other tables we can filter on. Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.storage); Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform); Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.status); Join<StorageUnitEntity, BusinessObjectDataEntity> businessObjectDataEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.businessObjectData); Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> businessObjectDataStatusEntity = businessObjectDataEntityJoin.join(BusinessObjectDataEntity_.status); // Get the current time. Timestamp currentTime = new Timestamp(System.currentTimeMillis()); // Create the standard restrictions (i.e. the standard where clauses). // Restrictions include: // - Storage platform is set to S3 storage // - Storage unit status is DISABLED // - Associated BData has a DELETED status // - Final destroy on timestamp < current time List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storagePlatformEntityJoin.get(StoragePlatformEntity_.name), StoragePlatformEntity.S3)); predicates.add(builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.DISABLED)); predicates.add(builder.equal(businessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code), BusinessObjectDataStatusEntity.DELETED)); predicates.add(builder.lessThan(storageUnitEntityRoot.get(StorageUnitEntity_.finalDestroyOn), currentTime)); // Order the results. Order orderBy = builder.asc(storageUnitEntityRoot.get(StorageUnitEntity_.finalDestroyOn)); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and return the results. return entityManager.createQuery(criteria).setMaxResults(maxResult).getResultList(); }
Example 20
Source File: SortField.java From activejpa with Apache License 2.0 | 4 votes |
protected <T extends Model> Order getOrder(CriteriaBuilder builder, Root<T> root) { Path<?> path = getPath(root, name); return asc ? builder.asc(path) : builder.desc(path); }