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.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getActiveTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    Root<TVShow> show = query.from(TVShow.class);
    query.select(criteria.countDistinct(show));
    query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("active count: " + count);
    return count;
}

From source file:br.com.sementesdoamanha.repository.Servidores.java

public List<Servidor> filtrados(ServidorFilter filtro) {
    //select, from, where, like... --> select(), from(), where()
    //JPQL: from Servidor

    //JPQL: select s from Servidor s where c.nome like = 'Joo%' and c.cpf = like '046.244.901-77'
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Servidor> criteriaQuery = builder.createQuery(Servidor.class);
    Root<Servidor> s = criteriaQuery.from(Servidor.class);
    criteriaQuery.select(s);

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

    if (StringUtils.isNotBlank(filtro.getCpf())) {
        predicates.add(builder.equal(s.<String>get("cpf"), filtro.getCpf()));
    }/*from   ww  w .  ja  v  a  2 s.  c om*/
    if (StringUtils.isNotBlank(filtro.getNome())) {
        predicates
                .add(builder.like(builder.upper(s.<String>get("nome")), filtro.getNome().toUpperCase() + "%"));

    }

    criteriaQuery.where(predicates.toArray(new Predicate[0]));
    TypedQuery<Servidor> query = manager.createQuery(criteriaQuery);
    return query.getResultList();

}

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

public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters,
        List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class);
    Root<Promotion> root = criteriaQuery.from(Promotion.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (hasBegun != null) {
        if (hasBegun) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(),
                    criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date())));
        } else {/*from   w  w  w  .  j  a  v  a2s.c  om*/
            restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(),
                    criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date()));
        }
    }
    if (hasEnded != null) {
        if (hasEnded) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:org.wallride.repository.CategoryRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Category> root = query.from(Category.class);
    query.select(root.get(Category_.id));
    query.where(cb.equal(root.get(Category_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:ro.alexdutescu.coduribare.service.AbstractFacade.java

public int count() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(getEntityManager().getCriteriaBuilder().count(rt));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return em.createQuery(cq).getResultList();
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);/*from  w w  w  . ja  va2  s. c  o m*/
    return q.getResultList();
}

From source file:org.sloth.persistence.impl.UserDaoImpl.java

@Override
public Collection<User> getAll() {
    CriteriaQuery<User> cq = getEntityManager().getCriteriaBuilder().createQuery(User.class);
    cq.select(cq.from(User.class));
    Collection<User> list = getEntityManager().createQuery(cq).getResultList();
    logger.info("Getting all Users; Found: {}", list.size());
    return list;/* w w  w. j  a va  2 s . co  m*/
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static int viewListCount(User user, String moduleResourceUri, String cloudServiceName)
        throws ConfigurationException, ValidationException {
    int count = 0;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {/*from   ww  w . jav  a 2 s . c  om*/
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Long> critQuery = builder.createQuery(Long.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(builder.count(rootQuery));
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        TypedQuery<Long> query = em.createQuery(critQuery);
        count = (int) (long) query.getSingleResult();
    } finally {
        em.close();
    }
    return count;
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

protected T findLast() {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = em.createQuery(cq);

    List<T> option = Optional.ofNullable(q.setMaxResults(1).getResultList()).orElse(new ArrayList());

    T entity = null;/*  w w w  .j  ava2  s  .c o  m*/
    if (Objects.nonNull(option.get(0))) {
        entity = option.get(0);
    }
    return entity;
}