Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:eu.uqasar.service.AbstractService.java

private <T extends Persistable> List<T> getRange(Class<T> clazz, int first, int count) {
    logger.infof("loading %d %s starting from %d  ...", count, getReadableClassName(clazz), first);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    query.from(clazz);//www .jav a  2 s .c o m
    return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList();
}

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

@Override
public List<Person> findByUnknownIdentifier(final String identifierValue) throws RepositoryAccessException {
    final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();

    final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class);
    final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class);
    final Join<JpaPersonImpl, JpaIdentifierImpl> identifier = person.join(JpaPersonImpl_.identifiers);

    c.select(person).distinct(true)// w w w .  j  a v a  2 s . com
            .where(criteriaBuilder.like(identifier.get(JpaIdentifierImpl_.value), identifierValue + "%"));

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

    return new ArrayList<Person>(persons);
}

From source file:eu.uqasar.service.AbstractService.java

public <T extends Persistable> List<T> getAll(Class<T> clazz) {
    logger.infof("loading all %ss ...", getReadableClassName(clazz));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = cb.createQuery(clazz);
    criteria.from(clazz);/* www. j  a v  a  2 s  .  c o  m*/
    return em.createQuery(criteria).getResultList();
}

From source file:eu.uqasar.service.AbstractService.java

private <T extends Persistable> List<T> getRangeOrdered(Class<T> clazz, int first, int count, Order... orders) {
    logger.infof("loading %d %s starting from %d  ...", count, getReadableClassName(clazz), first);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    query.from(clazz);/* w w w  .j a  va  2s  .  c  om*/
    if (orders != null && orders.length > 0) {
        query.orderBy(orders);
    }
    return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:eu.uqasar.service.AbstractService.java

public List<T> getAllExcept(Collection<T> toExclude) {
    if (toExclude == null || toExclude.isEmpty()) {
        return this.getAll();
    }//ww  w. j av a2s  .c  om
    logger.infof("loading all %s except %s ...", readableClassName, toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(this.clazz);
    Root<T> root = query.from(this.clazz);
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:eu.uqasar.service.AbstractService.java

public <T extends Persistable> List<T> getAllExcept(Class<T> clazz, Collection<T> toExclude) {
    if (toExclude == null || toExclude.isEmpty()) {
        return getAll(clazz);
    }//from   ww w.ja v  a2s.  c  o  m
    logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:eu.uqasar.service.AbstractService.java

protected Long performDistinctCountWithEqualPredicate(final Attribute<T, ?> attr, Object value) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<T> root = query.from(clazz);
    query.where(cb.equal(root.get(attr.getName()), value));
    query.select(cb.countDistinct(root));
    return em.createQuery(query).getSingleResult();
}

From source file:com.netflix.genie.core.jpa.services.JpaJobSearchServiceImpl.java

/**
 * {@inheritDoc}//from   w w  w  . j a v  a 2 s. com
 */
@Override
public Page<JobSearchResult> findJobs(final String id, final String jobName, final String user,
        final Set<JobStatus> statuses, final Set<String> tags, final String clusterName, final String clusterId,
        final String commandName, final String commandId, final Date minStarted, final Date maxStarted,
        final Date minFinished, final Date maxFinished, @NotNull final Pageable page) {
    log.debug("called");

    final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
    final Root<JobEntity> root = countQuery.from(JobEntity.class);

    final Predicate whereClause = JpaJobSpecs.getFindPredicate(root, cb, id, jobName, user, statuses, tags,
            clusterName, clusterId == null ? null : this.clusterRepository.findOne(clusterId), commandName,
            commandId == null ? null : this.commandRepository.findOne(commandId), minStarted, maxStarted,
            minFinished, maxFinished);

    countQuery.select(cb.count(root)).where(whereClause);

    final Long count = this.entityManager.createQuery(countQuery).getSingleResult();

    // Use the count to make sure we even need to make this query
    if (count > 0) {
        final CriteriaQuery<JobSearchResult> contentQuery = cb.createQuery(JobSearchResult.class);
        contentQuery.from(JobEntity.class);

        contentQuery.multiselect(root.get(JobEntity_.id), root.get(JobEntity_.name), root.get(JobEntity_.user),
                root.get(JobEntity_.status), root.get(JobEntity_.started), root.get(JobEntity_.finished),
                root.get(JobEntity_.clusterName), root.get(JobEntity_.commandName));

        contentQuery.where(whereClause);

        final Sort sort = page.getSort();
        final List<Order> orders = new ArrayList<>();
        sort.iterator().forEachRemaining(order -> {
            if (order.isAscending()) {
                orders.add(cb.asc(root.get(order.getProperty())));
            } else {
                orders.add(cb.desc(root.get(order.getProperty())));
            }
        });

        contentQuery.orderBy(orders);

        final List<JobSearchResult> results = this.entityManager.createQuery(contentQuery)
                .setFirstResult(page.getOffset()).setMaxResults(page.getPageSize()).getResultList();

        return new PageImpl<>(results, page, count);
    } else {
        return new PageImpl<>(Lists.newArrayList(), page, count);
    }
}

From source file:eu.uqasar.service.AbstractService.java

/**
 *
 * @return//  w  w w .  ja  v a 2  s  .  c  o m
 */
@SuppressWarnings("unused")
public List<T> getAll() {
    logger.infof("loading all %ss ...", readableClassName);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = cb.createQuery(clazz);
    Root<T> root = criteria.from(clazz);
    return em.createQuery(criteria).getResultList();
}

From source file:it.attocchi.jpa2.JPAEntityFilter.java

public Predicate getWherePredicate(Class<T> clazz, EntityManagerFactory emf) throws Exception {

    // CriteriaBuilder
    CriteriaBuilder criteriaBuilder = emf.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz);
    Root<T> root = criteriaQuery.from(clazz);

    criteriaQuery.select(root);/*  w ww . j a va  2  s. c  o  m*/

    List<Predicate> predicateList = new ArrayList<Predicate>();

    // return getWherePredicates(emf, predicateList, criteriaQuery,
    // criteriaBuilder, root);

    Predicate[] predicates = getWherePredicates(emf, predicateList, criteriaQuery, criteriaBuilder, root);
    Predicate wherePredicate = criteriaBuilder.and(predicates);

    return wherePredicate;
}