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:com.uni.dao.etc.UniJpaRepository.java

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

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

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

    return em.createQuery(query);
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

@Override
public <T extends BaseEntity> Long count(Class<T> entity) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {//from  w  w w  . j a  va 2s.c  o m
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Long> cq = cb.createQuery(Long.class);
        return em.createQuery(cq.select(cb.count(cq.from(entity)))).getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.uni.dao.etc.UniJpaRepository.java

/**
 * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}.
 * //from w  w w. j a v a 2 s . c o m
 * @param spec can be {@literal null}.
 * @param sort can be {@literal null}.
 * @return
 */
private TypedQuery<T> getQuery(Specification<T> spec, Sort sort) {

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

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

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

    return applyLockMode(em.createQuery(query));
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public Page<Product> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Product>(Collections.<Product>emptyList(), 0, pageable);
    }//from  w w w.jav a 2s  .co m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.join("favoriteMembers"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

private Query applicationServersForSuggestBox(String input) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> q = cb.createQuery(String.class);
    Root<ResourceEntity> r = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType");
    Predicate appServerNamePred = cb.like(resType.<String>get("name"),
            DefaultResourceTypeDefinition.APPLICATIONSERVER.name());
    input = input + "%";
    q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input)));
    q.select(r.<String>get("name"));
    q.distinct(true);/*  w  ww  .j a v a  2 s.co  m*/

    q.orderBy(cb.asc(r.get("name")));

    return entityManager.createQuery(q);
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public List<Product> findList(Goods goods, Set<Product> excludes) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (goods != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("goods"), goods));
    }/*  w  w  w  .j a v  a  2s .  c om*/
    if (excludes != null && !excludes.isEmpty()) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes)));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

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

/**
 * Create a TypedQuery from a request page
 * @param page request page//  ww w  .  j  a  v  a2  s  . c o  m
 * @return new TypedQuery
 */
@SuppressWarnings("unchecked")
private <K> TypedQuery<K> getCriteriaQuery(Page<K> page) {
    CriteriaQuery<K> criteria = getCriteria(page);

    CriteriaQuery<Long> countCriteria = (CriteriaQuery<Long>) getCriteria(page);
    CriteriaBuilder cb = em.getCriteriaBuilder();

    Root<?> root = countCriteria.getRoots().iterator().next();
    countCriteria.select(cb.count(root));

    page.setCount((em.createQuery(countCriteria).getSingleResult()).intValue());

    criteria.orderBy(getOrder(page, criteria));

    // Add default select to entity class if none was set.
    if (criteria.getSelection() == null) {
        criteria.select((Selection<? extends K>) root);
    }

    return em.createQuery(criteria);
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}//from   w  w  w . j  ava2s  .co  m
 */
@Override
public <T extends BaseEntity> List<T> findAll(Class<T> entity) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity);
        return em.createQuery(cq.select(cq.from(entity))).getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}//from ww w  .  j a  v  a 2  s  . co  m
 */
@Override
public <T extends BaseEntity> List<T> findPaginatedRecords(Class<T> entity, int startPos, int maxResult) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity);
        return em.createQuery(cq.select(cq.from(entity))).setFirstResult(startPos).setMaxResults(maxResult)
                .getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public List<Product> search(String keyword, Boolean isGift, Integer count) {
    if (StringUtils.isEmpty(keyword)) {
        return Collections.<Product>emptyList();
    }//from   w  ww. java 2s .  c  om
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (pattern.matcher(keyword).matches()) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("id"), Long.valueOf(keyword)),
                        criteriaBuilder.like(root.<String>get("sn"), "%" + keyword + "%"),
                        criteriaBuilder.like(root.<String>get("fullName"), "%" + keyword + "%")));
    } else {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.like(root.<String>get("sn"), "%" + keyword + "%"),
                        criteriaBuilder.like(root.<String>get("fullName"), "%" + keyword + "%")));
    }
    if (isGift != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isGift"), isGift));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, null, count, null, null);
}