Example usage for javax.persistence.criteria CriteriaQuery getRoots

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

Introduction

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

Prototype

Set<Root<?>> getRoots();

Source Link

Document

Return the query roots.

Usage

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Find the Root with type class on CriteriaQuery Root Set
 * @param <T> root type//from  ww w  .  j a  va2  s . c o  m
 * @param query criteria query
 * @param clazz root type
 * @return Root<T> of null if none
 */
@SuppressWarnings("unchecked")
public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) {

    for (Root<?> r : query.getRoots()) {
        if (clazz.equals(r.getJavaType())) {
            return (Root<T>) r.as(clazz);
        }
    }
    return (Root<T>) query.getRoots().iterator().next();
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Copy criteria without selection and order.
 * @param from source Criteria.//from   w  w  w .  j  ava 2  s. c  o  m
 * @param to destination Criteria.
 */
private static void copyCriteriaWithoutSelectionAndOrder(CriteriaQuery<?> from, CriteriaQuery<?> to) {
    if (isEclipseLink(from) && from.getRestriction() != null) {
        // EclipseLink adds roots from predicate paths to critera. Skip copying 
        // roots as workaround.
    } else {
        // Copy Roots
        for (Root<?> root : from.getRoots()) {
            Root<?> dest = to.from(root.getJavaType());
            dest.alias(getOrCreateAlias(root));
            copyJoins(root, dest);
        }
    }

    to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());

    if (from.getGroupRestriction() != null)
        to.having(from.getGroupRestriction());

    Predicate predicate = from.getRestriction();
    if (predicate != null)
        to.where(predicate);
}

From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java

@SuppressWarnings("unchecked")
private Root<Object> root(CriteriaQuery<?> criteriaQuery) {
    return (Root<Object>) criteriaQuery.getRoots().iterator().next();
}

From source file:org.jasig.jpa.BaseJpaDao.java

/**
 * Creates the cache region name for the criteria query
 * /*from  w w  w .j  a v a 2s .c  o m*/
 * @param criteriaQuery The criteria to create the cache name for
 */
protected final <T> String getCacheRegionName(CriteriaQuery<T> criteriaQuery) {
    final Set<Root<?>> roots = criteriaQuery.getRoots();
    final Class<?> cacheRegionType = roots.iterator().next().getJavaType();
    final String cacheRegion = cacheRegionType.getName() + QUERY_SUFFIX;

    if (roots.size() > 1) {
        logger.warn("Query " + criteriaQuery + " in " + this.getClass() + " has " + roots.size()
                + " roots. The first will be used to generated the cache region name: " + cacheRegion);
    }
    return cacheRegion;
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public List<T> find() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
    Root<T> customer = criteriaQuery.from(entityClass);
    criteriaQuery.getRoots().add(customer);
    Query query = em.createQuery(criteriaQuery);
    return query.getResultList();
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public List<T> find(int pageNo, int pageSize) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
    Root<T> customer = criteriaQuery.from(entityClass);
    criteriaQuery.getRoots().add(customer);
    Query query = em.createQuery(criteriaQuery);
    query.setFirstResult(pageNo * pageSize);
    query.setMaxResults(pageSize);/*from   ww  w  .j  av  a 2s .  c  om*/
    return query.getResultList();
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

private Root<T> getRoot(CriteriaQuery<?> criteriaQuery, Class<T> clazz) {
    if (criteriaQuery != null && criteriaQuery.getRoots() != null && clazz != null) {
        for (Root<?> root : criteriaQuery.getRoots()) {
            if (clazz.equals(root.getJavaType())) {
                return (Root<T>) root.as(clazz);
            }/*  w w w .j  a  va  2s. c  o m*/
        }
    }
    return null;
}

From source file:org.businessmanager.dao.em.FilteredEntityManagerImpl.java

/**
 * @param <T>//from ww  w . jav  a 2s  .c  o  m
 * @param theCriteriaQuery
 */
@SuppressWarnings("unchecked")
private <T> void appendIsNotDeletedClause(CriteriaQuery<T> theCriteriaQuery) {
    Root<T> aRootQuery = null;
    Path<Object> aPath = null;
    Iterator<Root<?>> anIterator = theCriteriaQuery.getRoots().iterator();
    if (anIterator.hasNext()) {
        aRootQuery = (Root<T>) anIterator.next();
        aPath = aRootQuery.get(AbstractEntity_.modificationType.getName());
    }
    Predicate aPredicate = getCriteriaBuilder().notEqual(aPath, ModificationType.DELETE);
    theCriteriaQuery.where(theCriteriaQuery.getRestriction(), aPredicate);
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    Assert.notNull(criteriaQuery);//from w w  w.  j a  v  a  2 s. c om
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    if (pageable == null) {
        pageable = new Pageable();
    }
    // CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    // Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, pageable);
    // addOrders(criteriaQuery, pageable);
    // if (criteriaQuery.getOrderList().isEmpty()) {
    // if (OrderEntity.class.isAssignableFrom(entityClass)) {
    // criteriaQuery.orderBy(criteriaBuilder.asc(root
    // .get(OrderEntity.ORDER_PROPERTY_NAME)));
    // } else {
    // criteriaQuery.orderBy(criteriaBuilder.desc(root
    // .get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
    // }
    // }
    long total = count(criteriaQuery, null);
    int totalPages = (int) Math.ceil(total / (double) pageable.getPageSize());
    if (totalPages < pageable.getPageNumber()) {
        pageable.setPageNumber(totalPages);
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
    query.setMaxResults(pageable.getPageSize());
    return new Page<T>(query.getResultList(), total, pageable);
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);/*from  w w  w  .j av  a2s .c  om*/
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    addRestrictions(criteriaQuery, filters);

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

    for (Root<?> root : criteriaQuery.getRoots()) {
        Root<?> dest = countCriteriaQuery.from(root.getJavaType());
        dest.alias(getAlias(root));
        copyJoins(root, dest);
    }

    Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType());
    countCriteriaQuery.select(criteriaBuilder.count(countRoot.get("id").<String>get("stcd")));

    if (criteriaQuery.getGroupList() != null) {
        countCriteriaQuery.groupBy(criteriaQuery.getGroupList());
    }
    if (criteriaQuery.getGroupRestriction() != null) {
        countCriteriaQuery.having(criteriaQuery.getGroupRestriction());
    }
    if (criteriaQuery.getRestriction() != null) {
        countCriteriaQuery.where(criteriaQuery.getRestriction());
    }
    return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}