Example usage for javax.persistence.criteria Root join

List of usage examples for javax.persistence.criteria Root join

Introduction

In this page you can find the example usage for javax.persistence.criteria Root join.

Prototype

<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create an inner join to the specified single-valued attribute.

Usage

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/* w ww  .j  a v a  2  s.  co m*/
 */
@Override
public EmrClusterDefinitionEntity getEmrClusterDefinitionByAltKey(String namespaceCd, String definitionName) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EmrClusterDefinitionEntity> criteria = builder.createQuery(EmrClusterDefinitionEntity.class);

    // The criteria root is the EMR cluster definition.
    Root<EmrClusterDefinitionEntity> emrClusterDefinition = criteria.from(EmrClusterDefinitionEntity.class);

    // Join to the other tables we can filter on.
    Join<EmrClusterDefinitionEntity, NamespaceEntity> namespace = emrClusterDefinition
            .join(EmrClusterDefinitionEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate namespaceRestriction = builder.equal(builder.upper(namespace.get(NamespaceEntity_.code)),
            namespaceCd.toUpperCase());
    Predicate definitionNameRestriction = builder.equal(
            builder.upper(emrClusterDefinition.get(EmrClusterDefinitionEntity_.name)),
            definitionName.toUpperCase());

    criteria.select(emrClusterDefinition).where(builder.and(namespaceRestriction, definitionNameRestriction));

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one EMR cluster definition with parameters {namespace=\"%s\", clusterDefinitionName=\"%s\"}.",
            namespace, definitionName));
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from   w ww  .j  av  a2 s.c  o  m*/
 */
@Override
public BusinessObjectDataNotificationRegistrationEntity getBusinessObjectDataNotificationByAltKey(
        BusinessObjectDataNotificationRegistrationKey key) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataNotificationRegistrationEntity> criteria = builder
            .createQuery(BusinessObjectDataNotificationRegistrationEntity.class);

    // The criteria root is the business object data notification registration entity.
    Root<BusinessObjectDataNotificationRegistrationEntity> businessObjectDataNotificationEntity = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> namespaceEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)),
            key.getNamespace().toUpperCase());
    queryRestriction = builder.and(queryRestriction,
            builder.equal(
                    builder.upper(businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.name)),
                    key.getNotificationName().toUpperCase()));

    criteria.select(businessObjectDataNotificationEntity).where(queryRestriction);

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one business object data notification registration with with parameters {namespace=\"%s\", notificationName=\"%s\"}.",
            key.getNamespace(), key.getNotificationName()));
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}//from ww w  .j  av a  2  s  .  c  om
 */
@Override
public List<BusinessObjectDataNotificationRegistrationKey> getBusinessObjectDataNotificationRegistrationKeys(
        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 data notification.
    Root<BusinessObjectDataNotificationRegistrationEntity> businessObjectDataNotificationEntity = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> namespaceEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);

    // Get the columns.
    Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code);
    Path<String> notificationNameColumn = businessObjectDataNotificationEntity
            .get(BusinessObjectDataNotificationRegistrationEntity_.name);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)),
            namespaceCode.toUpperCase());

    // Add the select clause.
    criteria.multiselect(namespaceCodeColumn, notificationNameColumn);

    // Add the where clause.
    criteria.where(queryRestriction);

    // Add the order by clause.
    criteria.orderBy(builder.asc(notificationNameColumn));

    // 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<BusinessObjectDataNotificationRegistrationKey> businessObjectDataNotificationKeys = new ArrayList<>();
    for (Tuple tuple : tuples) {
        BusinessObjectDataNotificationRegistrationKey businessObjectDataNotificationKey = new BusinessObjectDataNotificationRegistrationKey();
        businessObjectDataNotificationKeys.add(businessObjectDataNotificationKey);
        businessObjectDataNotificationKey.setNamespace(tuple.get(namespaceCodeColumn));
        businessObjectDataNotificationKey.setNotificationName(tuple.get(notificationNameColumn));
    }

    return businessObjectDataNotificationKeys;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from  ww w . j a  va  2s . c o m*/
 */
@Override
public List<BusinessObjectDataNotificationRegistrationEntity> getBusinessObjectDataNotificationRegistrations(
        String notificationEventTypeCode, BusinessObjectDataKey businessObjectDataKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataNotificationRegistrationEntity> criteria = builder
            .createQuery(BusinessObjectDataNotificationRegistrationEntity.class);

    // The criteria root is the business object data notification registration entity.
    Root<BusinessObjectDataNotificationRegistrationEntity> businessObjectDataNotificationEntity = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> namespaceEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, NotificationEventTypeEntity> notificationEventTypeEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.notificationEventType);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, FileTypeEntity> fileTypeEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.fileType, JoinType.LEFT);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(
            builder.upper(notificationEventTypeEntity.get(NotificationEventTypeEntity_.code)),
            notificationEventTypeCode.toUpperCase());
    queryRestriction = builder.and(queryRestriction,
            builder.equal(builder.upper(businessObjectDefinitionNamespaceEntity.get(NamespaceEntity_.code)),
                    businessObjectDataKey.getNamespace().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(
                    builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
                    businessObjectDataKey.getBusinessObjectDefinitionName().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.or(
                    builder.isNull(businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                    builder.equal(
                            builder.upper(businessObjectDataNotificationEntity
                                    .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                            businessObjectDataKey.getBusinessObjectFormatUsage().toUpperCase())));
    queryRestriction = builder.and(queryRestriction,
            builder.or(
                    builder.isNull(businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.fileType)),
                    builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
                            businessObjectDataKey.getBusinessObjectFormatFileType().toUpperCase())));
    queryRestriction = builder.and(queryRestriction, builder.or(
            builder.isNull(businessObjectDataNotificationEntity
                    .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion)),
            builder.equal(
                    businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion),
                    businessObjectDataKey.getBusinessObjectFormatVersion())));

    // Order the results by namespace and notification name.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(namespaceEntity.get(NamespaceEntity_.code)));
    orderBy.add(builder.asc(
            businessObjectDataNotificationEntity.get(BusinessObjectDataNotificationRegistrationEntity_.name)));

    // Add the clauses for the query.
    criteria.select(businessObjectDataNotificationEntity).where(queryRestriction).orderBy(orderBy);

    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}//ww w.  jav a 2  s.  com
 */
@Override
@Cacheable(DaoSpringModuleConfig.DM_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.AbstractHerdDao.java

/**
 * TODO This method may be bdata specific. Consider creating new abstract class to group all bdata related DAO. Builds a sub-query to select the maximum
 * business object data version.//from  w  ww.  j  a  v  a  2  s  .  c  o  m
 *
 * @param builder the criteria builder
 * @param criteria the criteria query
 * @param businessObjectDataEntity the business object data entity that appears in the from clause of the main query
 * @param businessObjectFormatEntity the business object format entity that appears in the from clause of the main query
 * @param businessObjectDataStatus the business object data status
 * @param storageNames the list of storage names where the business object data storage units should be looked for (case-insensitive)
 * @param storagePlatformType the optional storage platform type, e.g. S3 for Hive DDL. It is ignored when the list of storages is not empty
 * @param excludedStoragePlatformType the optional storage platform type to be excluded from search. It is ignored when the list of storages is not empty or
 * the storage platform type is specified
 * @param selectOnlyAvailableStorageUnits specifies if only available storage units will be selected or any storage units regardless of their status
 *
 * @return the sub-query to select the maximum business object data version
 */
protected Subquery<Integer> getMaximumBusinessObjectDataVersionSubQuery(CriteriaBuilder builder,
        CriteriaQuery<?> criteria, From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        From<?, BusinessObjectFormatEntity> businessObjectFormatEntity, String businessObjectDataStatus,
        List<String> storageNames, String storagePlatformType, String excludedStoragePlatformType,
        boolean selectOnlyAvailableStorageUnits) {
    // Business object data version is not specified, so get the latest one in the specified storage.
    Subquery<Integer> subQuery = criteria.subquery(Integer.class);

    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery.from(BusinessObjectDataEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataEntity, StorageUnitEntity> subStorageUnitEntity = subBusinessObjectDataEntity
            .join(BusinessObjectDataEntity_.storageUnits);
    Join<StorageUnitEntity, StorageEntity> subStorageEntity = subStorageUnitEntity
            .join(StorageUnitEntity_.storage);
    Join<StorageEntity, StoragePlatformEntity> subStoragePlatformEntity = subStorageEntity
            .join(StorageEntity_.storagePlatform);
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity
            .join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<StorageUnitEntity, StorageUnitStatusEntity> subStorageUnitStatusEntity = subStorageUnitEntity
            .join(StorageUnitEntity_.status);

    // Add a standard restriction on business object format.
    Predicate subQueryRestriction = builder.equal(subBusinessObjectFormatEntity, businessObjectFormatEntity);

    // Create and add standard restrictions on primary and sub-partition values.
    subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder,
            subBusinessObjectDataEntity, businessObjectDataEntity));

    // If specified, create and add a standard restriction on business object data status.
    if (businessObjectDataStatus != null) {
        Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity
                .join(BusinessObjectDataEntity_.status);

        subQueryRestriction = builder.and(subQueryRestriction,
                builder.equal(
                        builder.upper(
                                subBusinessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)),
                        businessObjectDataStatus.toUpperCase()));
    }

    // Create and add a standard restriction on storage.
    subQueryRestriction = builder.and(subQueryRestriction,
            getQueryRestrictionOnStorage(builder, subStorageEntity, subStoragePlatformEntity, storageNames,
                    storagePlatformType, excludedStoragePlatformType));

    // If specified, add a restriction on storage unit status availability flag.
    if (selectOnlyAvailableStorageUnits) {
        subQueryRestriction = builder.and(subQueryRestriction,
                builder.isTrue(subStorageUnitStatusEntity.get(StorageUnitStatusEntity_.available)));
    }

    subQuery.select(builder.max(subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.version)))
            .where(subQueryRestriction);

    return subQuery;
}

From source file:org.finra.herd.dao.impl.AttributeValueListDaoImpl.java

@Override
public AttributeValueListEntity getAttributeValueListByKey(AttributeValueListKey attributeValueListKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<AttributeValueListEntity> criteria = builder.createQuery(AttributeValueListEntity.class);

    // The criteria root is the attribute value list.
    Root<AttributeValueListEntity> attributeValueListEntityRoot = criteria.from(AttributeValueListEntity.class);

    // Join to the other tables we can filter on.
    Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityRoot
            .join(AttributeValueListEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)),
            attributeValueListKey.getNamespace().toUpperCase()));
    predicates//from   www  .  j ava 2  s  .c o  m
            .add(builder.equal(builder.upper(attributeValueListEntityRoot.get(AttributeValueListEntity_.name)),
                    attributeValueListKey.getAttributeValueListName().toUpperCase()));

    // Add all clauses to the query.
    criteria.select(attributeValueListEntityRoot)
            .where(builder.and(predicates.toArray(new Predicate[predicates.size()])));

    // Execute the query and return the results.
    return executeSingleResultQuery(criteria, String.format(
            "Found more than one attribute value list with parameters {namespace=\"%s\", attribute_value_name=\"%s\"}.",
            attributeValueListKey.getNamespace(), attributeValueListKey.getAttributeValueListName()));
}

From source file:org.finra.herd.dao.impl.AttributeValueListDaoImpl.java

@Override
public List<AttributeValueListKey> getAttributeValueLists(Collection<String> namespaces) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<AttributeValueListEntity> criteria = builder.createQuery(AttributeValueListEntity.class);

    // The criteria root is the attribute value list entity.
    Root<AttributeValueListEntity> attributeValueListEntityRoot = criteria.from(AttributeValueListEntity.class);

    // Join to the other tables we can filter on.
    Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityRoot
            .join(AttributeValueListEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(namespaces)) {
        predicates.add(namespaceEntityJoin.get(NamespaceEntity_.code).in(namespaces));
    }// w w w.  j a va  2 s  . c  o m

    // Order the results by namespace and job name.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(namespaceEntityJoin.get(NamespaceEntity_.code)));
    orderBy.add(builder.asc(attributeValueListEntityRoot.get(AttributeValueListEntity_.name)));

    // Add all clauses to the query.
    criteria.select(attributeValueListEntityRoot)
            .where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy);

    // Execute the query and build a list of keys.
    List<AttributeValueListKey> attributeValueListKeys = new ArrayList<>();
    for (AttributeValueListEntity attributeValueListEntity : entityManager.createQuery(criteria)
            .getResultList()) {
        attributeValueListKeys.add(new AttributeValueListKey(attributeValueListEntity.getNamespace().getCode(),
                attributeValueListEntity.getName()));
    }

    return attributeValueListKeys;
}

From source file:org.finra.herd.dao.impl.BusinessObjectDataAttributeDaoImpl.java

@Override
public BusinessObjectDataAttributeEntity getBusinessObjectDataAttributeByKey(
        BusinessObjectDataAttributeKey businessObjectDataAttributeKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataAttributeEntity> criteria = builder
            .createQuery(BusinessObjectDataAttributeEntity.class);

    // The criteria root is the business object data attribute.
    Root<BusinessObjectDataAttributeEntity> businessObjectDataAttributeEntityRoot = criteria
            .from(BusinessObjectDataAttributeEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataAttributeEntity, BusinessObjectDataEntity> businessObjectDataEntityJoin = businessObjectDataAttributeEntityRoot
            .join(BusinessObjectDataAttributeEntity_.businessObjectData);
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityJoin
            .join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectFormatEntityJoin
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntityJoin = businessObjectFormatEntityJoin
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntityJoin = businessObjectDefinitionEntityJoin
            .join(BusinessObjectDefinitionEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)),
            businessObjectDataAttributeKey.getNamespace().toUpperCase()));
    predicates.add(builder.equal(/*  w  w  w  .  j a  v  a 2  s . c  om*/
            builder.upper(businessObjectDefinitionEntityJoin.get(BusinessObjectDefinitionEntity_.name)),
            businessObjectDataAttributeKey.getBusinessObjectDefinitionName().toUpperCase()));
    predicates.add(builder.equal(
            builder.upper(businessObjectDefinitionEntityJoin.get(BusinessObjectDefinitionEntity_.name)),
            businessObjectDataAttributeKey.getBusinessObjectDefinitionName().toUpperCase()));
    predicates.add(
            builder.equal(builder.upper(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.usage)),
                    businessObjectDataAttributeKey.getBusinessObjectFormatUsage().toUpperCase()));
    predicates.add(builder.equal(builder.upper(fileTypeEntityJoin.get(FileTypeEntity_.code)),
            businessObjectDataAttributeKey.getBusinessObjectFormatFileType().toUpperCase()));
    predicates.add(builder.equal(
            businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectFormatVersion),
            businessObjectDataAttributeKey.getBusinessObjectFormatVersion()));
    predicates.add(getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntityJoin,
            businessObjectDataAttributeKey.getPartitionValue(),
            businessObjectDataAttributeKey.getSubPartitionValues()));
    predicates.add(builder.equal(businessObjectDataEntityJoin.get(BusinessObjectDataEntity_.version),
            businessObjectDataAttributeKey.getBusinessObjectDataVersion()));
    predicates.add(builder.equal(
            builder.upper(businessObjectDataAttributeEntityRoot.get(BusinessObjectDataAttributeEntity_.name)),
            businessObjectDataAttributeKey.getBusinessObjectDataAttributeName().toUpperCase()));

    // Add the clauses for the query.
    criteria.select(businessObjectDataAttributeEntityRoot)
            .where(builder.and(predicates.toArray(new Predicate[predicates.size()])));

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one business object data attribute instance with parameters {namespace=\"%s\", businessObjectDefinitionName=\"%s\","
                    + " businessObjectFormatUsage=\"%s\", businessObjectFormatFileType=\"%s\", businessObjectFormatVersion=\"%d\","
                    + " businessObjectDataPartitionValue=\"%s\", businessObjectDataSubPartitionValues=\"%s\", businessObjectDataVersion=\"%d\","
                    + " businessObjectDataAttributeName=\"%s\"}.",
            businessObjectDataAttributeKey.getNamespace(),
            businessObjectDataAttributeKey.getBusinessObjectDefinitionName(),
            businessObjectDataAttributeKey.getBusinessObjectFormatUsage(),
            businessObjectDataAttributeKey.getBusinessObjectFormatFileType(),
            businessObjectDataAttributeKey.getBusinessObjectFormatVersion(),
            businessObjectDataAttributeKey.getPartitionValue(),
            CollectionUtils.isEmpty(businessObjectDataAttributeKey.getSubPartitionValues()) ? ""
                    : StringUtils.join(businessObjectDataAttributeKey.getSubPartitionValues(), ","),
            businessObjectDataAttributeKey.getBusinessObjectDataVersion(),
            businessObjectDataAttributeKey.getBusinessObjectDataAttributeName()));
}

From source file:org.finra.herd.dao.impl.BusinessObjectDataDaoImpl.java

@Override
public BusinessObjectDataEntity getBusinessObjectDataByAltKeyAndStatus(
        BusinessObjectDataKey businessObjectDataKey, String businessObjectDataStatus) {
    // 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> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class);

    // Join to other tables that we need to filter on.
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate mainQueryRestriction = getQueryRestriction(builder, businessObjectDataEntity,
            businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectDataKey);

    // If a format version was specified, use the latest available for this partition value.
    if (businessObjectDataKey.getBusinessObjectFormatVersion() == null) {
        // Business object format version is not specified, so just use the latest available for this set of partition values.
        Subquery<Integer> subQuery = criteria.subquery(Integer.class);

        // The criteria root is the business object data.
        Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery
                .from(BusinessObjectDataEntity.class);

        // Join to the other tables we can filter on.
        Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity
                .join(BusinessObjectDataEntity_.businessObjectFormat);
        Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> subBusinessObjectDefinitionEntity = subBusinessObjectFormatEntity
                .join(BusinessObjectFormatEntity_.businessObjectDefinition);
        Join<BusinessObjectFormatEntity, FileTypeEntity> subBusinessObjectFormatFileTypeEntity = subBusinessObjectFormatEntity
                .join(BusinessObjectFormatEntity_.fileType);
        Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity
                .join(BusinessObjectDataEntity_.status);

        // Create the standard restrictions (i.e. the standard where clauses).
        Predicate subQueryRestriction = builder.equal(subBusinessObjectDefinitionEntity,
                businessObjectDefinitionEntity);
        subQueryRestriction = builder.and(subQueryRestriction,
                builder.equal(subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage),
                        businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)));
        subQueryRestriction = builder.and(subQueryRestriction,
                builder.equal(subBusinessObjectFormatFileTypeEntity, fileTypeEntity));

        // Create and add standard restrictions on primary and sub-partition values.
        subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder,
                subBusinessObjectDataEntity, businessObjectDataEntity));

        // Add restrictions on business object data version and business object data status.
        Predicate subQueryRestrictionOnBusinessObjectDataVersionAndStatus = getQueryRestrictionOnBusinessObjectDataVersionAndStatus(
                builder, subBusinessObjectDataEntity, subBusinessObjectDataStatusEntity,
                businessObjectDataKey.getBusinessObjectDataVersion(), businessObjectDataStatus);
        if (subQueryRestrictionOnBusinessObjectDataVersionAndStatus != null) {
            subQueryRestriction = builder.and(subQueryRestriction,
                    subQueryRestrictionOnBusinessObjectDataVersionAndStatus);
        }//from w ww  . java  2s .c o  m

        subQuery.select(builder.max(
                subBusinessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)))
                .where(subQueryRestriction);

        mainQueryRestriction = builder.and(mainQueryRestriction,
                builder.in(
                        businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion))
                        .value(subQuery));
    }

    // If a data version was not specified, use the latest one as per specified business object data status.
    if (businessObjectDataKey.getBusinessObjectDataVersion() == null) {
        // Since business object data version is not specified, just use the latest one as per specified business object data status.
        if (businessObjectDataStatus != null) {
            // Business object data version is not specified, so get the latest one as per specified business object data status.
            Subquery<Integer> subQuery = criteria.subquery(Integer.class);

            // The criteria root is the business object data.
            Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subQuery
                    .from(BusinessObjectDataEntity.class);

            // Join to the other tables we can filter on.
            Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> subBusinessObjectDataStatusEntity = subBusinessObjectDataEntity
                    .join(BusinessObjectDataEntity_.status);
            Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> subBusinessObjectFormatEntity = subBusinessObjectDataEntity
                    .join(BusinessObjectDataEntity_.businessObjectFormat);

            // Create the standard restrictions (i.e. the standard where clauses).
            Predicate subQueryRestriction = builder.equal(subBusinessObjectFormatEntity,
                    businessObjectFormatEntity);

            // Create and add standard restrictions on primary and sub-partition values.
            subQueryRestriction = builder.and(subQueryRestriction, getQueryRestrictionOnPartitionValues(builder,
                    subBusinessObjectDataEntity, businessObjectDataEntity));

            // Create and add standard restrictions on business object data status.
            subQueryRestriction = builder.and(subQueryRestriction, builder.equal(
                    builder.upper(subBusinessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)),
                    businessObjectDataStatus.toUpperCase()));

            subQuery.select(builder.max(subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.version)))
                    .where(subQueryRestriction);

            mainQueryRestriction = builder.and(mainQueryRestriction, builder
                    .in(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)).value(subQuery));
        } else {
            // Both business object data version and business object data status are not specified, so just use the latest business object data version.
            mainQueryRestriction = builder.and(mainQueryRestriction,
                    builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.latestVersion), true));
        }
    }

    criteria.select(businessObjectDataEntity).where(mainQueryRestriction);

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one business object data instance with parameters {namespace=\"%s\", businessObjectDefinitionName=\"%s\","
                    + " businessObjectFormatUsage=\"%s\", businessObjectFormatFileType=\"%s\", businessObjectFormatVersion=\"%d\","
                    + " businessObjectDataPartitionValue=\"%s\", businessObjectDataSubPartitionValues=\"%s\", businessObjectDataVersion=\"%d\","
                    + " businessObjectDataStatus=\"%s\"}.",
            businessObjectDataKey.getNamespace(), businessObjectDataKey.getBusinessObjectDefinitionName(),
            businessObjectDataKey.getBusinessObjectFormatUsage(),
            businessObjectDataKey.getBusinessObjectFormatFileType(),
            businessObjectDataKey.getBusinessObjectFormatVersion(), businessObjectDataKey.getPartitionValue(),
            CollectionUtils.isEmpty(businessObjectDataKey.getSubPartitionValues()) ? ""
                    : StringUtils.join(businessObjectDataKey.getSubPartitionValues(), ","),
            businessObjectDataKey.getBusinessObjectDataVersion(), businessObjectDataStatus));
}