List of usage examples for javax.persistence Tuple get
Object get(int i);
From source file:com.evanzeimet.queryinfo.jpa.result.AbstractTupleToPojoQueryInfoResultConverter.java
protected QueryInfoResultType convertTuple(Map<String, MethodHandle> methodHandles, Tuple tuple) throws Throwable { QueryInfoResultType result = getBaseInstanceFactory().create(); Iterator<Entry<String, MethodHandle>> methodHandlesEntryIterator = methodHandles.entrySet().iterator(); while (methodHandlesEntryIterator.hasNext()) { Entry<String, MethodHandle> methodHandleEntry = methodHandlesEntryIterator.next(); String elementAlias = methodHandleEntry.getKey(); Object value = tuple.get(elementAlias); MethodHandle methodHandle = methodHandleEntry.getValue(); methodHandle.invoke(result, value); }//from w w w.j a va 2 s.c o m return result; }
From source file:org.apache.ambari.server.orm.dao.ServiceConfigDAO.java
@RequiresSession public List<ServiceConfigEntity> getLastServiceConfigVersionsForGroups(Collection<Long> configGroupIds) { if (configGroupIds == null || configGroupIds.isEmpty()) { return Collections.emptyList(); }/*from w w w . j a v a 2 s. com*/ CriteriaBuilder cb = entityManagerProvider.get().getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<ServiceConfigEntity> groupVersion = cq.from(ServiceConfigEntity.class); cq.multiselect(groupVersion.get("groupId").alias("groupId"), cb.max(groupVersion.<Long>get("version")).alias("lastVersion")); cq.where(groupVersion.get("groupId").in(configGroupIds)); cq.groupBy(groupVersion.get("groupId")); List<Tuple> tuples = daoUtils.selectList(entityManagerProvider.get().createQuery(cq)); List<ServiceConfigEntity> result = new ArrayList<ServiceConfigEntity>(); //subquery look to be very poor, no bulk select then, cache should help here as result size is naturally limited for (Tuple tuple : tuples) { CriteriaQuery<ServiceConfigEntity> sce = cb.createQuery(ServiceConfigEntity.class); Root<ServiceConfigEntity> sceRoot = sce.from(ServiceConfigEntity.class); sce.where(cb.and(cb.equal(sceRoot.get("groupId"), tuple.get("groupId")), cb.equal(sceRoot.get("version"), tuple.get("lastVersion")))); sce.select(sceRoot); result.add(daoUtils.selectSingle(entityManagerProvider.get().createQuery(sce))); } return result; }
From source file:org.easy.test.criteria.CriteriaProcessorTest.java
/** * select count(course.id) from course/*w ww.ja va2s . co m*/ */ @Test public void testSelectCount() { CriteriaComposer<Course> forCourse = CriteriaComposer.from(Course.class).select(COUNT, Course_.id); Tuple result = criteriaProcessor.findUniqueTuple(forCourse); assertNotNull(result); assertEquals(3L, result.get("count.Course.id")); }
From source file:org.easy.test.criteria.CriteriaProcessorTest.java
/** * sum(course.unit) from course// w ww.j av a2 s . c o m */ @Test public void testSelectSum() { CriteriaComposer<Course> forCourse = CriteriaComposer.from(Course.class).select(SUM, Course_.unit); Tuple result = criteriaProcessor.findUniqueTuple(forCourse); assertNotNull(result); assertEquals(10L, result.get("sum.Course.unit")); }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}//from w w w . ja v a 2 s . c om */ @Override public List<BusinessObjectDefinitionKey> getBusinessObjectDefinitions(String namespaceCode) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the business object definition. Root<BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = criteria .from(BusinessObjectDefinitionEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Get the columns. Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code); Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); // Add the select clause. criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn); // If namespace code is specified, add the where clause. if (StringUtils.isNotBlank(namespaceCode)) { criteria.where(builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), namespaceCode.toUpperCase())); } // Add the order by clause. if (StringUtils.isNotBlank(namespaceCode)) { criteria.orderBy(builder.asc(namespaceCodeColumn), builder.asc(businessObjectDefinitionNameColumn)); } else { criteria.orderBy(builder.asc(businessObjectDefinitionNameColumn)); } // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row). List<BusinessObjectDefinitionKey> businessObjectDefinitionKeys = new ArrayList<>(); for (Tuple tuple : tuples) { BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey(); businessObjectDefinitionKeys.add(businessObjectDefinitionKey); businessObjectDefinitionKey.setNamespace(tuple.get(namespaceCodeColumn)); businessObjectDefinitionKey .setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn)); } return businessObjectDefinitionKeys; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}//w w w . j a v a2 s . c o m */ @Override public List<BusinessObjectFormatKey> getBusinessObjectFormats( BusinessObjectDefinitionKey businessObjectDefinitionKey, boolean latestBusinessObjectFormatVersion) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the business object format. Root<BusinessObjectFormatEntity> businessObjectFormatEntity = criteria .from(BusinessObjectFormatEntity.class); // Join to the other tables we can filter on. Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Get the columns. Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code); Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); Path<String> businessObjectFormatUsageColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.usage); Path<String> fileTypeCodeColumn = fileTypeEntity.get(FileTypeEntity_.code); Path<Integer> businessObjectFormatVersionColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.businessObjectFormatVersion); Expression<Integer> maxBusinessObjectFormatVersionExpression = builder .max(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectDefinitionKey.getNamespace().toUpperCase()); queryRestriction = builder.and(queryRestriction, builder.equal( builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)), businessObjectDefinitionKey.getBusinessObjectDefinitionName().toUpperCase())); // Add the select clause. criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn, businessObjectFormatUsageColumn, fileTypeCodeColumn, latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn); // Add the where clause. criteria.where(queryRestriction); // If only the latest (maximum) business object format versions to be returned, create and apply the group by clause. if (latestBusinessObjectFormatVersion) { List<Expression<?>> grouping = new ArrayList<>(); grouping.add(namespaceCodeColumn); grouping.add(businessObjectDefinitionNameColumn); grouping.add(businessObjectFormatUsageColumn); grouping.add(fileTypeCodeColumn); criteria.groupBy(grouping); } // Add the order by clause. List<Order> orderBy = new ArrayList<>(); orderBy.add(builder.asc(businessObjectFormatUsageColumn)); orderBy.add(builder.asc(fileTypeCodeColumn)); if (!latestBusinessObjectFormatVersion) { orderBy.add(builder.asc(businessObjectFormatVersionColumn)); } criteria.orderBy(orderBy); // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row). List<BusinessObjectFormatKey> businessObjectFormatKeys = new ArrayList<>(); for (Tuple tuple : tuples) { BusinessObjectFormatKey businessObjectFormatKey = new BusinessObjectFormatKey(); businessObjectFormatKeys.add(businessObjectFormatKey); businessObjectFormatKey.setNamespace(tuple.get(namespaceCodeColumn)); businessObjectFormatKey.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn)); businessObjectFormatKey.setBusinessObjectFormatUsage(tuple.get(businessObjectFormatUsageColumn)); businessObjectFormatKey.setBusinessObjectFormatFileType(tuple.get(fileTypeCodeColumn)); businessObjectFormatKey.setBusinessObjectFormatVersion( tuple.get(latestBusinessObjectFormatVersion ? maxBusinessObjectFormatVersionExpression : businessObjectFormatVersionColumn)); } return businessObjectFormatKeys; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * {@inheritDoc}/*from w w w .jav a 2 s . com*/ */ @Override public List<CustomDdlKey> getCustomDdls(BusinessObjectFormatKey businessObjectFormatKey) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the business object format. Root<CustomDdlEntity> customDdlEntity = criteria.from(CustomDdlEntity.class); // Join to the other tables we can filter on. Join<CustomDdlEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = customDdlEntity .join(CustomDdlEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.fileType); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Get the columns. Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code); Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); Path<String> businessObjectFormatUsageColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.usage); Path<String> fileTypeCodeColumn = fileTypeEntity.get(FileTypeEntity_.code); Path<Integer> businessObjectFormatVersionColumn = businessObjectFormatEntity .get(BusinessObjectFormatEntity_.businessObjectFormatVersion); Path<String> customDdlNameColumn = customDdlEntity.get(CustomDdlEntity_.customDdlName); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectFormatKey.getNamespace().toUpperCase()); queryRestriction = builder.and(queryRestriction, builder.equal( builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)), businessObjectFormatKey.getBusinessObjectDefinitionName().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)), businessObjectFormatKey.getBusinessObjectFormatUsage().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)), businessObjectFormatKey.getBusinessObjectFormatFileType().toUpperCase())); queryRestriction = builder.and(queryRestriction, builder.equal( businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion), businessObjectFormatKey.getBusinessObjectFormatVersion())); // Add the select clause. criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn, businessObjectFormatUsageColumn, fileTypeCodeColumn, businessObjectFormatVersionColumn, customDdlNameColumn); // Add the where clause. criteria.where(queryRestriction); // Add the order by clause. criteria.orderBy(builder.asc(customDdlNameColumn)); // Run the query to get a list of tuples back. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row). List<CustomDdlKey> customDdlKeys = new ArrayList<>(); for (Tuple tuple : tuples) { CustomDdlKey customDdlKey = new CustomDdlKey(); customDdlKeys.add(customDdlKey); customDdlKey.setNamespace(tuple.get(namespaceCodeColumn)); customDdlKey.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn)); customDdlKey.setBusinessObjectFormatUsage(tuple.get(businessObjectFormatUsageColumn)); customDdlKey.setBusinessObjectFormatFileType(tuple.get(fileTypeCodeColumn)); customDdlKey.setBusinessObjectFormatVersion(tuple.get(businessObjectFormatVersionColumn)); customDdlKey.setCustomDdlName(tuple.get(customDdlNameColumn)); } return customDdlKeys; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * TODO: Make this method the main body of getStorageUploadStats once we migrate away from Oracle getStorageUploadStats TODO: that leverages the storage * file view to query. This method is meant to be database agnostic. * * @param storageAlternateKey the storage alternate key * @param dateRange the date range//from w w w . ja v a 2 s.c o m * * @return the upload statistics */ private StorageDailyUploadStats getStorageUploadStatsDatabaseAgnostic( StorageAlternateKeyDto storageAlternateKey, DateRangeDto dateRange) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); Root<StorageFileViewEntity> storageFileViewEntity = criteria.from(StorageFileViewEntity.class); Path<Date> createdDate = storageFileViewEntity.get(StorageFileViewEntity_.createdDate); Expression<Long> totalFilesExpression = builder .count(storageFileViewEntity.get(StorageFileViewEntity_.storageFileId)); Expression<Long> totalBytesExpression = builder .sum(storageFileViewEntity.get(StorageFileViewEntity_.fileSizeInBytes)); Predicate storageNameRestriction = builder.equal( builder.upper(storageFileViewEntity.get(StorageFileViewEntity_.storageCode)), storageAlternateKey.getStorageName().toUpperCase()); Predicate createDateRestriction = builder.and( builder.greaterThanOrEqualTo(createdDate, dateRange.getLowerDate()), builder.lessThanOrEqualTo(createdDate, dateRange.getUpperDate())); criteria.multiselect(createdDate, totalFilesExpression, totalBytesExpression); criteria.where(builder.and(storageNameRestriction, createDateRestriction)); List<Expression<?>> grouping = new ArrayList<>(); grouping.add(createdDate); criteria.groupBy(grouping); criteria.orderBy(builder.asc(createdDate)); // 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(DmDateUtils.getXMLGregorianCalendarValue(tuple.get(createdDate))); uploadStat.setTotalFiles(tuple.get(totalFilesExpression)); uploadStat.setTotalBytes(tuple.get(totalBytesExpression)); } return uploadStats; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.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 .java 2s . 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( DmDateUtils.getXMLGregorianCalendarValue(tuple.get(truncCreatedOnDateExpression))); uploadStat.setTotalFiles(tuple.get(totalFilesExpression)); uploadStat.setTotalBytes(tuple.get(totalBytesExpression)); } return uploadStats; }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * TODO: Make this method the main body of getStorageUploadStatsByBusinessObjectDefinition once we migrate away from Oracle TODO: * getStorageUploadStatsByBusinessObjectDefinition that uses storage file view. This method is meant to be database agnostic. * * @param storageAlternateKey the storage alternate key * @param dateRange the date range//from www . j a va 2 s .co m * * @return the upload statistics */ private StorageBusinessObjectDefinitionDailyUploadStats getStorageUploadStatsByBusinessObjectDefinitionDatabaseAgnostic( StorageAlternateKeyDto storageAlternateKey, DateRangeDto dateRange) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); Root<StorageFileViewEntity> storageFileViewEntity = criteria.from(StorageFileViewEntity.class); Path<String> namespaceCode = storageFileViewEntity.get(StorageFileViewEntity_.namespaceCode); Path<String> dataProviderCode = storageFileViewEntity.get(StorageFileViewEntity_.dataProviderCode); Path<String> businessObjectDefinitionName = storageFileViewEntity .get(StorageFileViewEntity_.businessObjectDefinitionName); Path<Date> createdDate = storageFileViewEntity.get(StorageFileViewEntity_.createdDate); Expression<Long> totalFilesExpression = builder .count(storageFileViewEntity.get(StorageFileViewEntity_.storageFileId)); Expression<Long> totalBytesExpression = builder .sum(storageFileViewEntity.get(StorageFileViewEntity_.fileSizeInBytes)); Predicate storageNameRestriction = builder.equal( builder.upper(storageFileViewEntity.get(StorageFileViewEntity_.storageCode)), storageAlternateKey.getStorageName().toUpperCase()); Predicate createDateRestriction = builder.and( builder.greaterThanOrEqualTo(createdDate, dateRange.getLowerDate()), builder.lessThanOrEqualTo(createdDate, dateRange.getUpperDate())); criteria.multiselect(createdDate, namespaceCode, dataProviderCode, businessObjectDefinitionName, totalFilesExpression, totalBytesExpression); criteria.where(builder.and(storageNameRestriction, createDateRestriction)); // Create the group by clause. List<Expression<?>> grouping = new ArrayList<>(); grouping.add(createdDate); grouping.add(namespaceCode); grouping.add(dataProviderCode); grouping.add(businessObjectDefinitionName); criteria.groupBy(grouping); // Create the order by clause. criteria.orderBy(builder.asc(createdDate), builder.asc(namespaceCode), builder.asc(dataProviderCode), builder.asc(businessObjectDefinitionName)); // Retrieve and return the storage upload statistics. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); StorageBusinessObjectDefinitionDailyUploadStats uploadStats = new StorageBusinessObjectDefinitionDailyUploadStats(); for (Tuple tuple : tuples) { StorageBusinessObjectDefinitionDailyUploadStat uploadStat = new StorageBusinessObjectDefinitionDailyUploadStat(); uploadStats.getStorageBusinessObjectDefinitionDailyUploadStats().add(uploadStat); uploadStat.setUploadDate(DmDateUtils.getXMLGregorianCalendarValue(tuple.get(createdDate))); uploadStat.setNamespace(tuple.get(namespaceCode)); uploadStat.setDataProviderName(tuple.get(dataProviderCode)); uploadStat.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionName)); uploadStat.setTotalFiles(tuple.get(totalFilesExpression)); uploadStat.setTotalBytes(tuple.get(totalBytesExpression)); } return uploadStats; }