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

protected List<T> getAllOrdered(Class<T> clazz, Order... orders) {
    logger.infof("loading all %ss ...", getReadableClassName(clazz));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = cb.createQuery(clazz);
    criteria.from(clazz);//  ww  w.j a v a 2s . c  o m
    if (orders != null && orders.length > 0) {
        criteria.orderBy(orders);
    }
    return em.createQuery(criteria).getResultList();
}

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

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

/**
 *
 * @return/*from ww  w.  ja va  2  s.com*/
 */
public long countAll() {
    logger.infof("counting all %ss ...", readableClassName);
    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();
}

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

protected <T extends Persistable> List<T> getAllOrderedExcept(Class<T> clazz, Collection<T> toExclude,
        Order... orders) {/*from www. ja v a  2 s  .  co m*/
    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: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));/*ww  w . ja va  2 s  .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:dk.dma.msinm.service.CategoryService.java

/**
 * Looks up an category by name//from w ww .jav  a2s  . c o m
 * @param name the name to search for
 * @param lang the language. Optional
 * @param parentId the parent ID. Optional
 * @return The matching category, or null if not found
 */
public Category findByName(String name, String lang, Integer parentId) {
    // Sanity check
    if (StringUtils.isBlank(name)) {
        return null;
    }

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Category> categoryQuery = builder.createQuery(Category.class);

    Root<Category> categoryRoot = categoryQuery.from(Category.class);

    // Build the predicate
    PredicateHelper<Category> predicateBuilder = new PredicateHelper<>(builder, categoryQuery);

    // Match the name
    Join<Category, CategoryDesc> descs = categoryRoot.join("descs", JoinType.LEFT);
    predicateBuilder.like(descs.get("name"), name);
    // Optionally, match the language
    if (StringUtils.isNotBlank(lang)) {
        predicateBuilder.equals(descs.get("lang"), lang);
    }

    // Optionally, match the parent
    if (parentId != null) {
        categoryRoot.join("parent", JoinType.LEFT);
        Path<Category> parent = categoryRoot.get("parent");
        predicateBuilder.equals(parent.get("id"), parentId);
    }

    // Complete the query
    categoryQuery.select(categoryRoot).distinct(true).where(predicateBuilder.where());

    // Execute the query and update the search result
    List<Category> result = em.createQuery(categoryQuery).getResultList();

    return result.size() > 0 ? result.get(0) : null;
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Override
public List<ENTITY> find(Collection<KEY> ids) {
    if (log.isDebugEnabled()) {
        log.debug("Finding by ids " + entityClass + "#" + ids);
    }/*from  w ww .  j av  a 2  s . c  o m*/
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<ENTITY> criteriaQuery = cb.createQuery(entityClass);
    Root<ENTITY> from = criteriaQuery.from(entityClass);
    criteriaQuery.where(from.in(ids));
    TypedQuery<ENTITY> query = entityManager.createQuery(criteriaQuery);
    return findList(query);
}

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

/**
 * Creates a new count query for the given {@link Specification}.
 * /*from  ww  w .j a va  2  s . c om*/
 * @param spec can be {@literal null}.
 * @return
 */
private TypedQuery<Long> getCountQuery(Specification<T> spec) {

    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(builder.count(root)).distinct(true);

    return getEntityManager().createQuery(query);
}

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

/**
 * Creates a new {@link TypedQuery} from the given {@link Specification}.
 * /* w  w w.  j  a  v  a2s  .  c  o  m*/
 * @param spec can be {@literal null}
 * @param pageable can be {@literal null}
 * @return
 */
private TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) {

    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(getDomainClass());

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(root);

    if (pageable != null) {
        query.orderBy(toOrders(pageable.getSort(), root, builder));
    }

    return getEntityManager().createQuery(query);
}

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

public CriteriaQuery<T> getCriteria(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);/*from   w  w w.  j  a v  a 2s.c o m*/

    // criteria.where( builder.equal( personRoot.get( Utente_.eyeColor ),
    // "brown" ) );

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

    // buildWhere(emf, predicateList, criteriaQuery, criteriaBuilder, root);
    //
    // Predicate[] predicates = new Predicate[predicateList.size()];
    // predicateList.toArray(predicates);

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

    // buildSort(criteriaQuery, criteriaBuilder, root);
    List<Order> orders = new ArrayList<Order>();
    buildSort(orders, criteriaQuery, criteriaBuilder, root);
    if (ListUtils.isNotEmpty(orders))
        criteriaQuery.orderBy(orders);

    return criteriaQuery;
}