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:de.egore911.persistence.selector.AbstractSelector.java

public long count() {
    EntityManager em = EntityManagerUtil.getEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<T> from = cq.from(getEntityClass());
    List<Predicate> predicates = generatePredicateList(builder, from, cq);
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    cq.select(builder.count(from));/* w  ww. j  av a  2  s . c o  m*/
    TypedQuery<Long> q = em.createQuery(cq);
    return q.getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/*from w  w w. j a va2 s .co m*/
 */
public List<EntityType> retrieveAll(Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    return entityManager.createQuery(query).getResultList();
}

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

@Override
@Transactional(readOnly = true)/*from   w ww.j av a  2  s.  c o m*/
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;
}

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 {/* ww  w . j a v  a  2s.c o m*/
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/*from  ww  w . j  a  va 2  s  . co m*/
 */
public EntityType findExact(String key, Object value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

    return entityManager.createQuery(query).getSingleResult();
}

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)//w w w  . jav  a  2 s .  co 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:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/*ww w.j  a v  a  2 s. co m*/
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(FIELD_CONSTANT_ID), id));

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/*  w ww.ja  v a 2  s  .com*/
 */
public List<EntityType> findExactList(String key, IDType value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

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

}

From source file:com.datapine.dao.impl.UserDAOImpl.java

/**
 * Looks for the user by email.//w w w  . java2 s. com
 * @param email The specified email.
 * @return Found user.
 */
@Override
public final User findByEmail(final String email) {
    final CriteriaBuilder builder = this.manager.getCriteriaBuilder();
    final CriteriaQuery<User> query = builder.createQuery(User.class);
    final Root<User> root = query.from(User.class);
    query.where(builder.equal(root.get("email"), email));
    return this.manager.createQuery(query).getResultList().get(0);
}

From source file:com.datapine.dao.impl.UserDAOImpl.java

/**
 * Gets an iterator for the collection of all users sorted by id.
 * @return An iterator./*  w  w w.ja v a 2 s.c  o m*/
 */
@Override
public final Iterator<User> findAllOrderById() {
    final CriteriaBuilder builder = this.manager.getCriteriaBuilder();
    final CriteriaQuery<User> query = builder.createQuery(User.class);
    final Root<User> root = query.from(User.class);
    query.select(root).orderBy(builder.asc(root.get("id")));
    return this.manager.createQuery(query).getResultList().iterator();
}