Example usage for javax.persistence.criteria CriteriaQuery from

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

Introduction

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

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

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);/*from ww  w.ja  va2  s.c  o  m*/
    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));
    }
    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.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);// w w  w. j  a v a  2s  . com
    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));
    }
    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:csns.model.core.dao.jpa.UserDaoImpl.java

@Override
public List<User> getUsers(Long ids[]) {
    if (ids == null || ids.length < 1)
        return new ArrayList<User>();

    CriteriaBuilder cbuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cquery = cbuilder.createQuery(User.class);
    Root<User> user = cquery.from(User.class);

    Predicate criteria = cbuilder.equal(user.get("id"), ids[0]);
    for (int i = 1; i < ids.length; ++i)
        criteria = cbuilder.or(criteria, cbuilder.equal(user.get("id"), ids[i]));
    cquery.where(criteria);/*from  w w w  . j  a  va 2 s .co  m*/

    cquery.orderBy(cbuilder.asc(user.get("lastName")), cbuilder.asc(user.get("firstName")));

    return entityManager.createQuery(cquery).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  . ja  v  a  2  s .co m*/
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

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)/*from  w  w w. j a v a  2  s .c o  m*/
            .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 {
        return results.get(0);
    }

}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java

@Override
public List<YhteystietoElementti> findByLisatietoIdAndKentanNimi(String yhteystietojenTyyppiOid,
        String kentanNimi) {/*from   ww  w .ja v a2s  .co m*/

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

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

    Predicate tyyppiEquals = cb.equal(root.get("yhteystietojenTyyppi").get("oid"), yhteystietojenTyyppiOid);
    Predicate nameEquals = cb.equal(root.get("nimi"), kentanNimi);

    Predicate whereClause = cb.and(tyyppiEquals, nameEquals);
    query.where(whereClause);

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

    //        YhteystietojenTyyppi ytT =  yttDao.findBy("oid", yhteystietojenTyyppiOid).get(0);
    //        Query query = getEntityManager().createQuery(
    //                "SELECT ltk FROM YhteystietoElementti ltk " +
    //                "WHERE ltk.yhteystietojenTyyppi.id = :yhteystietojenTyyppiId " +
    //                "AND ltk.nimi = :kentanNimi"
    //                );
    //        return query
    //                .setParameter("yhteystietojenTyyppiId", ytT.getId())
    //                .setParameter("kentanNimi", kentanNimi)
    //                .getResultList();
}

From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java

@Override
@Transactional(readOnly = true)/*from w  w  w.  j a  va 2s .c o  m*/
public ExampleEnum findMaxEnum() {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleEnum> criteriaQuery = builder.createQuery(ExampleEnum.class);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);

    criteriaQuery.select(root.get(ExampleGeneratedIdEntity_.exampleEnum));

    final List<ExampleEnum> enums = this.getEntityManager().createQuery(criteriaQuery).getResultList();
    ExampleEnum max = null;
    for (final ExampleEnum exampleEnum : enums) {
        if (max == null || exampleEnum.ordinal() > max.ordinal()) {
            max = exampleEnum;
        }
    }

    return max;
}

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

@Override
public User getByMail(String mail) {
    if (mail == null) {
        throw new NullPointerException();
    }//from  w  w  w. j  a va 2s  .  co  m
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> user = cq.from(User.class);
    cq.select(user).where(cb.equal(user.get(User_.mail), mail));
    User result = null;
    try {
        result = getEntityManager().createQuery(cq).getSingleResult();
    } catch (NoResultException e) {
        logger.info("User with address {} not found", mail);
    } catch (NonUniqueResultException e) {
        logger.warn("Corrupt Database", e);
    }
    return result;
}

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

@Override
public <T> List<T> getAllObjects(Class clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(clazz);
    Root<T> object = cq.from(clazz);
    cq.select(object);//from   w  w w  .ja va2  s  . c  om
    TypedQuery<T> q = em.createQuery(cq);
    return q.getResultList();
}

From source file:com.home.ln_spring.ch10.service.jpa.ContactServiceImplOld.java

@Override
@Transactional(readOnly = true)/*w  ww . ja  va2s  .c om*/
public List<Contact> findByCriteriaQuery(String firstName, String lastName) {
    log.info("Finding contact for firstName: " + firstName + " and lastName: " + lastName);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Contact> criteriaQuery = cb.createQuery(Contact.class);
    Root<Contact> contactRoot = criteriaQuery.from(Contact.class);
    contactRoot.fetch(Contact_.contactTelDetails, JoinType.LEFT);
    contactRoot.fetch(Contact_.hobbies, JoinType.LEFT);

    criteriaQuery.select(contactRoot).distinct(true);

    Predicate criteria = cb.conjunction();
    if (firstName != null) {
        Predicate p = cb.equal(contactRoot.get(Contact_.firstName), firstName);
        criteria = cb.and(criteria, p);
    }

    if (lastName != null) {
        Predicate p = cb.equal(contactRoot.get(Contact_.lastName), lastName);
        criteria = cb.and(criteria, p);
    }

    criteriaQuery.where(criteria);
    List<Contact> result = em.createQuery(criteriaQuery).getResultList();
    return result;
}