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:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java

@Override
public List<YhteystietoElementti> findAllKaytossa() {
    //        Query query = getEntityManager().createQuery("SELECT x FROM YhteystietoElementti x where x.kaytossa = true");
    //        return query.getResultList();

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class);

    Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class);
    query.select(root);

    Predicate whereClause = cb.equal(root.get("kaytossa"), true);
    query.where(whereClause);/*from  www . j  a  v  a2  s.  c o  m*/

    return getEntityManager().createQuery(query).getResultList();
}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findRandomStalledTask() {

    Calendar TOO_LONG_AGO = Calendar.getInstance();
    TOO_LONG_AGO.add(Calendar.SECOND, -7200);

    // select qth from QueuedTask where 
    //      qth.startedStamp != null AND
    //      qth.startedStamp < TOO_LONG_AGO
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth).where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.completedStamp)),
            cb.lessThan(qth.get(QueuedTaskHolder_.startedStamp), TOO_LONG_AGO)));

    List<QueuedTaskHolder> stalledTasks = this.entityManager.createQuery(cq).getResultList();

    if (stalledTasks.isEmpty()) {
        return null;
    } else {//from w w w  .j av  a  2 s  . c  o m
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

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

public Page<Consultation> findPage(Member member, Product product, Boolean isShow, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }//from   w  w w  . j  av a 2s  .com
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public Long count(Member member, Product product, Boolean isShow) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }//from   w  w  w  .j a  va2s  . c o  m
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findNextTaskForExecution() {

    Calendar NOW = Calendar.getInstance();

    // select qt from QueuedTask where
    //      qt.startedStamp == null AND
    //      (qth.triggerStamp == null || qth.triggerStamp < NOW)
    // order by qth.version ASC, qt.creationStamp ASC
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth)
            .where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.startedStamp)),
                    cb.or(cb.isNull(qth.get(QueuedTaskHolder_.triggerStamp)),
                            cb.lessThan(qth.get(QueuedTaskHolder_.triggerStamp), NOW))))
            .orderBy(cb.asc(qth.get(QueuedTaskHolder_.version)),
                    cb.asc(qth.get(QueuedTaskHolder_.creationStamp)));

    List<QueuedTaskHolder> results = this.entityManager.createQuery(cq).setMaxResults(1).getResultList();
    if (results.isEmpty()) {
        return null;
    } else {/*w w w  . j a v a2 s.co m*/
        return results.get(0);
    }

}

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

public List<Consultation> findList(Member member, Product product, Boolean isShow, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
    Root<Consultation> root = criteriaQuery.from(Consultation.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }/* w  w  w . j av a2 s.c  o m*/
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:com.sapito.db.dao.AbstractDao.java

public List<T> findAll() {
    if (this.entityClass == Producto.class) {
        entityManager.clear();/* ww  w .j  a v  a2  s  . com*/
    }
    javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return entityManager.createQuery(cq).getResultList();
}

From source file:com.sapito.db.dao.AbstractDao.java

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

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

private Collection<Report> get(boolean processed) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.processed), processed));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} {} Reports.", (processed) ? "processed" : "unprocessed", result.size());
    return result;
}

From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java

public int getPackageListingCount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Number> query = cb.createQuery(Number.class);
    query.select(cb.count(query.from(CRANPackageEntity.class)));
    return entityManager.createQuery(query).getSingleResult().intValue();
}