Example usage for javax.persistence.criteria CriteriaQuery from

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

Introduction

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

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

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  w ww .  j a v  a2  s .  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:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Override
public List<ENTITY> findAll() {
    if (log.isDebugEnabled()) {
        log.debug("Finding all " + entityClass);
    }//from  w  w w.  ja  v a  2s . c om
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ENTITY> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    criteriaQuery.from(entityClass);
    TypedQuery<ENTITY> query = entityManager.createQuery(criteriaQuery);
    return findList(query);
}

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

/**
 *
 * @return//www .  j  a  va  2s. c om
 */
@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:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

public Long findFilteredCount(String search, List<String> searchProperties) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<ENTITY> from = query.from(entityClass);
    query.select(builder.count(from));/*from   ww w.j a  v a2s  . com*/
    if (!Strings.isNullOrEmpty(search)) {
        Predicate[] buildFilterPredicates = BuildFilterPredicates(from, search, searchProperties);
        if (buildFilterPredicates.length > 0) {
            query.where(builder.or(buildFilterPredicates));
        }
    }
    return count(entityManager.createQuery(query));
}

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

protected <T extends Persistable> List<T> getAllOrderedExcept(Class<T> clazz, Collection<T> toExclude,
        Order... orders) {//from   w  w w. j  a  va 2 s  . c om
    if (toExclude == null || toExclude.isEmpty()) {
        return getAll(clazz);
    }
    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);
    if (orders != null && orders.length > 0) {
        query.orderBy(orders);
    }
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

/**
 * Applies the given {@link Specification} to the given
 * {@link CriteriaQuery}./*from  w ww .j a  va  2s. c  om*/
 * 
 * @param spec can be {@literal null}
 * @param query
 * @return
 */
private <S> Root<T> applySpecificationToCriteria(Specification<T> spec, CriteriaQuery<S> query) {

    Assert.notNull(query);
    Root<T> root = query.from(getDomainClass());

    if (spec == null) {
        return root;
    }

    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    Predicate predicate = spec.toPredicate(root, query, builder);

    if (predicate != null) {
        query.where(predicate);
    }

    return root;
}

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

public List<T> getAllExcept(Collection<T> toExclude) {
    if (toExclude == null || toExclude.isEmpty()) {
        return this.getAll();
    }//from   w w  w  . j  a va 2s.  c  o  m
    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

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:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

@VisibleForTesting
int getNbEntriesBeforeSlicing(EntityManager entityManager, Class<E> entityClass,
        CriteriaBuilder criteriaBuilder, Predicate[] predicates) {

    CriteriaQuery<Long> counterQuery = criteriaBuilder.createQuery(Long.class);

    CriteriaQuery<Long> select = counterQuery.select(criteriaBuilder.count(counterQuery.from(entityClass)));

    if (predicates.length > 0) {
        select.where(predicates);/*from  w w w. ja  va2 s  . c om*/
    }

    return entityManager.createQuery(counterQuery).getSingleResult().intValue();
}

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

public <T extends Persistable> long countAll(Class<T> clazz) {
    logger.infof("counting all %ss ...", getReadableClassName(clazz));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<T> root = criteria.from(clazz);
    criteria.select(cb.countDistinct(root));
    return em.createQuery(criteria).getSingleResult();
}