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:eu.uqasar.service.ProcessService.java

/**
 * //w  ww. j a va 2 s. c  o  m
 * @return
 */
public List<Process> getAllProcesses() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> query = cb.createQuery(Process.class);
    query.from(Process.class);
    return em.createQuery(query).getResultList();
}

From source file:net.shopxx.dao.impl.ArticleDaoImpl.java

public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication,
        Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);/*w  w w  . j a va  2  s  .co  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (articleCategory != null) {
        Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class);
        Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory),
                criteriaBuilder.like(subqueryRoot.<String>get("treePath"),
                        "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.in(root.get("articleCategory")).value(subquery));
    }
    if (isPublication != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isPublication"), isPublication));
    }
    if (generateMethod != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("generateMethod"), generateMethod));
    }
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, first, count, null, null);
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public List<T> getAll() {
    Validate.notNull(getClassEntity(), "Null ClassEntity parameter");
    CriteriaBuilder cB = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> cQ = cB.createQuery(getClassEntity());
    Root<T> t = cQ.from(getClassEntity());
    return getEntityManager().createQuery(cQ).getResultList();

}

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

@Override
public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass,
        List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes,
        List<SetAttribute> setAttributes) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true);
    Root<T> root = cq.from(returnClass);

    if (CollectionUtils.isNotEmpty(singleAttributes)) {
        for (SingularAttribute attribute : singleAttributes) {
            root.join(attribute);//from   w  w  w.  j a  v  a  2s .c  o m
            root.fetch(attribute);
        }
    }

    if (CollectionUtils.isNotEmpty(listAttributes)) {
        for (ListAttribute attribute : listAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }

    if (CollectionUtils.isNotEmpty(setAttributes)) {
        for (SetAttribute attribute : setAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }
    Set<Entry<String, Object>> set = map.entrySet();
    int numberOfClauses = set.size();
    Predicate[] predicates = new Predicate[numberOfClauses];
    int i = 0;
    for (Entry<String, Object> entry : set) {
        String key = entry.getKey();
        if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) {
            predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE);
        } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) {
            //predicates[i++] = cb.equal( ,entry.getValue());
        } else {
            predicates[i++] = cb.equal(root.get(key), entry.getValue());
        }
    }

    return em.createQuery(cq.select(root).where(predicates)).getResultList();

}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public List<Queue> fetchAllQueues() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Queue> q = cb.createQuery(Queue.class);
    return entityManager.createQuery(q.select(q.from(Queue.class))).getResultList();
}

From source file:eu.uqasar.service.ProcessService.java

/**
 * //from   w w w  .  j  av  a2 s  . co  m
 * @param processId
 * @return
 */
public boolean processExists(Long processId) {
    logger.info(String.format("checking if process with ID %d exists ...", processId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    criteria.where(cb.equal(from.get(Process_.id), processId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

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

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public List<E> findAll(final SingularAttribute<? super E, ?> attribute, final boolean asc) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    if (asc) {//from   w w  w.  j a  v a  2  s  .  c  o m
        criteriaQuery.orderBy(builder.asc(from.get(attribute)));
    } else {
        criteriaQuery.orderBy(builder.desc(from.get(attribute)));
    }

    return this.find(criteriaQuery);
}

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

public Page<Message> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class);
    Root<Message> root = criteriaQuery.from(Message.class);
    criteriaQuery.select(root);/*from w  w  w.  ja v  a  2 s  .  com*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forMessage")),
            criteriaBuilder.equal(root.get("isDraft"), false));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(
                        criteriaBuilder.and(criteriaBuilder.equal(root.get("sender"), member),
                                criteriaBuilder.equal(root.get("senderDelete"), false)),
                        criteriaBuilder.and(criteriaBuilder.equal(root.get("receiver"), member),
                                criteriaBuilder.equal(root.get("receiverDelete"), false))));
    } else {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(
                        criteriaBuilder.and(criteriaBuilder.isNull(root.get("sender")),
                                criteriaBuilder.equal(root.get("senderDelete"), false)),
                        criteriaBuilder.and(criteriaBuilder.isNull(root.get("receiver")),
                                criteriaBuilder.equal(root.get("receiverDelete"), false))));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);

}

From source file:eu.uqasar.service.ProcessService.java

/**
 * //  w w w . j  ava 2s. co  m
 * @param first
 * @param count
 * @return
 */
public List<Process> getAllByAscendingName(int first, int count) {
    logger.infof("loading all Processes ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    criteria.orderBy(cb.asc(root.get(Process_.name)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:eu.uqasar.service.ProcessService.java

public long countAllFiltered(final ProcessesFilterStructure filter) {
    logger.infof("counting all Processes matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*ww w  .  j  a  v a 2s.c om*/
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}