Example usage for javax.persistence.criteria CriteriaQuery select

List of usage examples for javax.persistence.criteria CriteriaQuery select

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery select.

Prototype

CriteriaQuery<T> select(Selection<? extends T> selection);

Source Link

Document

Specify the item that is to be returned in the query result.

Usage

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

/**
 * {@inheritDoc}//from w  w w .j  av  a  2s  . com
 */
@Override
public List<StorageFileEntity> getStorageFilesByStorageAndBusinessObjectData(StorageEntity storageEntity,
        List<BusinessObjectDataEntity> businessObjectDataEntities) {
    // 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);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(storageUnitEntity.get(StorageUnitEntity_.storage),
            storageEntity);
    queryRestriction = builder.and(queryRestriction, getPredicateForInClause(builder,
            storageUnitEntity.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntities));

    // Order the results by storage file path.
    Order orderBy = builder.asc(storageFileEntity.get(StorageFileEntity_.path));

    // Add the clauses for the query.
    criteria.select(storageFileEntity).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}//from   www .  jav  a2s . c  o m
 */
@Override
public List<StorageFileEntity> getStorageFileEntities(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);

    return entityManager.createQuery(criteria).getResultList();
}

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

/**
 * {@inheritDoc}//  ww w. j  a va2 s . c  om
 */
@Override
public Long getStorageFileCount(String storageName, String filePathPrefix) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.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 path.
    Expression<Long> storageFileCount = builder.count(storageFileEntity.get(StorageFileEntity_.id));

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());
    Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path),
            String.format("%s%%", filePathPrefix));

    // Add the clauses for the query.
    criteria.select(storageFileCount).where(builder.and(storageNameRestriction, filePathRestriction));

    return entityManager.createQuery(criteria).getSingleResult();
}

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

/**
 * {@inheritDoc}//from   w  w w.j  a va 2s. co m
 */
@Override
public BusinessObjectDefinitionEntity getBusinessObjectDefinitionByKey(
        BusinessObjectDefinitionKey businessObjectDefinitionKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDefinitionEntity> criteria = builder
            .createQuery(BusinessObjectDefinitionEntity.class);

    // 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);

    // 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()));

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

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one business object definition with parameters {namespace=\"%s\", businessObjectDefinitionName=\"%s\"}.",
            businessObjectDefinitionKey.getNamespace(),
            businessObjectDefinitionKey.getBusinessObjectDefinitionName()));
}

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

/**
 * {@inheritDoc}/*from   w  ww  . ja  v a  2s .  co  m*/
 */
@Override
public List<BusinessObjectDataEntity> getBusinessObjectDataEntities(
        BusinessObjectDataKey businessObjectDataKey) {
    // 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 the other tables we can 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 queryRestriction = getQueryRestriction(builder, businessObjectDataEntity,
            businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectDataKey);

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

    // Order by business object format and data versions.
    criteria.orderBy(
            builder.asc(
                    businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)),
            builder.asc(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)));

    return entityManager.createQuery(criteria).getResultList();
}

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

/**
 * {@inheritDoc}// ww  w .ja v a  2  s.co  m
 */
@Override
public Integer getBusinessObjectFormatMaxVersion(BusinessObjectFormatKey businessObjectFormatKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);

    // 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, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);

    // Create the standard restrictions (i.e. the standard where clauses).
    // Business object format version should be ignored when building the query restriction.
    Predicate queryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity,
            businessObjectDefinitionEntity, businessObjectFormatKey, true);

    // Create the path.
    Expression<Integer> maxBusinessObjectFormatVersion = builder
            .max(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion));

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

    return entityManager.createQuery(criteria).getSingleResult();
}

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

/**
 * {@inheritDoc}//from   ww w.j  a  v a2s  .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 w  w w .  ja  v  a 2s.c om*/
 */
@Override
public BusinessObjectFormatEntity getBusinessObjectFormatByAltKey(
        BusinessObjectFormatKey businessObjectFormatKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectFormatEntity> criteria = builder.createQuery(BusinessObjectFormatEntity.class);

    // 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, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);

    // Create the standard restrictions (i.e. the standard where clauses).
    // Please note that we specify not to ignore the business object format version.
    Predicate queryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity,
            businessObjectDefinitionEntity, businessObjectFormatKey, false);

    // If a business format version was not specified, use the latest one.
    if (businessObjectFormatKey.getBusinessObjectFormatVersion() == null) {
        queryRestriction = builder.and(queryRestriction,
                builder.isTrue(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.latestVersion)));
    }

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

    return executeSingleResultQuery(criteria,
            String.format("Found more than one business object format instance with parameters "
                    + "{namespace=\"%s\", businessObjectDefinitionName=\"%s\", businessObjectFormatUsage=\"%s\", businessObjectFormatFileType=\"%s\", "
                    + "businessObjectFormatVersion=\"%d\"}.", businessObjectFormatKey.getNamespace(),
                    businessObjectFormatKey.getBusinessObjectDefinitionName(),
                    businessObjectFormatKey.getBusinessObjectFormatUsage(),
                    businessObjectFormatKey.getBusinessObjectFormatFileType(),
                    businessObjectFormatKey.getBusinessObjectFormatVersion()));
}

From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java

public List<Person> searchByCriteria(final SearchCriteria searchCriteria) throws RepositoryAccessException {
    final String givenName = searchCriteria.getGivenName();
    final String familyName = searchCriteria.getFamilyName();
    final Date birthDate = searchCriteria.getDateOfBirth();
    final String searchCriteriaName = searchCriteria.getName();

    // search by role criteria
    final String sponsorNetID = searchCriteria.getSponsorNetID();
    final OrganizationalUnit organizationalUnit = searchCriteria.getOrganizationalUnit();
    final Date roleExpDate = searchCriteria.getRoleExpDate();
    final Type roleType = searchCriteria.getAffiliationType();

    final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();

    final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class);
    c.distinct(true);/*  w  w  w.j  a  v  a2  s.  c o  m*/
    final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class);
    final Join<JpaPersonImpl, JpaNameImpl> name = person.join(JpaPersonImpl_.names);
    final Join<JpaPersonImpl, JpaRoleImpl> role = person.join(JpaPersonImpl_.roles);

    //        person.fetch(JpaPersonImpl_.names);
    //        person.fetch(JpaPersonImpl_.roles);
    //        person.fetch(JpaPersonImpl_.identifiers);

    final Predicate pBirthDate;
    if (birthDate != null) {
        pBirthDate = criteriaBuilder.equal(person.get(JpaPersonImpl_.dateOfBirth), birthDate);
    } else {
        pBirthDate = null;
    }

    Predicate combined = null;

    if (StringUtils.hasText(givenName) && StringUtils.hasText(familyName)) {
        //          final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName );
        Expression<String> pattern = criteriaBuilder.literal((String) givenName + "%");
        final Predicate pGivenName = criteriaBuilder.like(name.get(JpaNameImpl_.given), pattern);
        final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName);

        combined = criteriaBuilder.and(pGivenName, pFamilyName);
    } else if (StringUtils.hasText(givenName)) {
        combined = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName);
    } else if (StringUtils.hasText(familyName)) {
        combined = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName);
    } else if (StringUtils.hasText(searchCriteriaName)) {
        final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), searchCriteriaName);
        final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), searchCriteriaName);
        combined = criteriaBuilder.or(pGivenName, pFamilyName);
    }

    Predicate pRoleCombined = null;

    if (roleType != null) {
        //final Join<JpaPersonImpl,JpaRoleImpl> role = person.join(JpaPersonImpl_.roles);
        //final Join<JpaPersonImpl,JpaRoleImpl> role = name.join(JpaPersonImpl_.roles);
        pRoleCombined = criteriaBuilder.equal(role.get(JpaRoleImpl_.affiliationType), roleType);
    }

    if (organizationalUnit != null && StringUtils.hasText(organizationalUnit.getName())) {
        Predicate orgUnitP = criteriaBuilder.equal(role.get(JpaRoleImpl_.organizationalUnit),
                organizationalUnit);
        if (pRoleCombined != null)
            pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP);
        else
            pRoleCombined = orgUnitP;
        //pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP);
    }

    if (StringUtils.hasText(sponsorNetID)) {
        Person person1 = findByIdentifier(Type.IdentifierTypes.NETID.name(), sponsorNetID);
        if (person1 != null) {
            Predicate sponsorP = criteriaBuilder.equal(role.get(JpaRoleImpl_.sponsorId), person1.getId());
            if (pRoleCombined != null)
                pRoleCombined = criteriaBuilder.and(pRoleCombined, sponsorP);
            else
                pRoleCombined = sponsorP;
        } else {
            pRoleCombined = criteriaBuilder.or();
        }

    }
    if (roleExpDate != null) {
        Predicate expDateP = criteriaBuilder.between(role.get(JpaRoleImpl_.end), new Date(), roleExpDate);
        if (pRoleCombined != null)
            pRoleCombined = criteriaBuilder.and(pRoleCombined, expDateP);
        else
            pRoleCombined = expDateP;
    }

    Predicate pComplete = criteriaBuilder.and();

    if (pBirthDate != null)
        pComplete = criteriaBuilder.and(pComplete, pBirthDate);
    if (combined != null)
        pComplete = criteriaBuilder.and(pComplete, combined);
    if (pRoleCombined != null)
        pComplete = criteriaBuilder.and(pComplete, pRoleCombined);

    c.select(person).where(pComplete);

    //        if (pBirthDate != null && combined != null) {
    //            c.select(person).where(criteriaBuilder.and(pBirthDate, combined));
    //        } else if (pBirthDate != null) {
    //            c.select(person).where(pBirthDate);
    //        } else {
    //            c.select(person).where(combined);
    //        }

    final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).setMaxResults(MAX_QUERY_LIMIT)
            .getResultList();

    return new ArrayList<Person>(persons);
}

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

/**
 * {@inheritDoc}//  w  w  w.  j av a2  s  .  c  o m
 */
@Override
public List<BusinessObjectDataEntity> getBusinessObjectDataFromStorageOlderThan(String storageName,
        int thresholdMinutes, List<String> businessObjectDataStatusesToIgnore) {
    // 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 the other tables we can filter on.
    Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.storageUnits);
    Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
    Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> businessObjectDataStatusEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.status);

    // Compute threshold timestamp based on the current database timestamp and threshold minutes.
    Timestamp thresholdTimestamp = subtractMinutes(getCurrentTimestamp(), thresholdMinutes);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());
    queryRestriction = builder.and(queryRestriction, builder.not(businessObjectDataStatusEntity
            .get(BusinessObjectDataStatusEntity_.code).in(businessObjectDataStatusesToIgnore)));
    queryRestriction = builder.and(queryRestriction, builder.lessThanOrEqualTo(
            businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn), thresholdTimestamp));

    // Order the results by file path.
    Order orderByCreatedOn = builder.asc(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn));

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

    return entityManager.createQuery(criteria).getResultList();
}