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.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * {@inheritDoc}/*ww  w.ja v  a 2  s. c o  m*/
 */
public BulkUpload findLastBulkUpload() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BulkUpload> query = builder.createQuery(BulkUpload.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(root);
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last bulkupload", e);
    }
    return null;
}

From source file:eu.domibus.common.dao.ErrorLogDao.java

public List<ErrorLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<ErrorLogEntry> cq = cb.createQuery(ErrorLogEntry.class);
    Root<ErrorLogEntry> ele = cq.from(ErrorLogEntry.class);
    cq.select(ele);
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "":
                        break;
                    case "timestampFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "timestampTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(ele.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }//w w w.  java  2  s .c  o m
                }
            } else {
                predicates.add(cb.equal(ele.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(ele.get(column)));
        } else {
            cq.orderBy(cb.desc(ele.get(column)));
        }

    }
    final TypedQuery<ErrorLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Find entities.//w  w  w  . j  a  v a2  s.co  m
 * 
 * @param em
 *            the em
 * @param target
 *            the target
 * @param all
 *            the all
 * @param maxResults
 *            the max results
 * @param firstResult
 *            the first result
 * @return the list
 * @since 28/08/2014, 11:20:27 PM
 */
private List<Entity> findEntities(EntityManager em, Class<Entity> target, boolean all, int maxResults,
        int firstResult) {
    CriteriaQuery<Entity> cq = em.getCriteriaBuilder().createQuery(target);
    cq.select(cq.from(target));
    TypedQuery<Entity> q = em.createQuery(cq);
    if (!all) {
        q.setMaxResults(maxResults);
        q.setFirstResult(firstResult);
    }
    return q.getResultList();
}

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

public Page<CouponCode> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class);
    Root<CouponCode> root = criteriaQuery.from(CouponCode.class);
    criteriaQuery.select(root);
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }// ww  w .  j a v  a  2s  . co m
    return super.findPage(criteriaQuery, pageable);
}

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

/**
 * {@inheritDoc}/*from   www.j a  v  a 2  s  .  c  o m*/
 */
public List<CreditCard> getCreditCards(String sortField, String sortOrder, Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CreditCard> query = builder.createQuery(CreditCard.class);
    Root<CreditCard> root = query.from(CreditCard.class);
    query.select(root);
    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(filters, builder, root, query);

    return super.find(query);
}

From source file:eu.uqasar.service.company.CompanyService.java

/**
 * /*from   w  w  w.j  a v a2s  .  c  o m*/
 * @param companyId
 * @return
 */
public boolean companyExists(Long companyId) {
    logger.info(String.format("checking if Company with ID %d exists ...", companyId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Company> from = criteria.from(Company.class);
    criteria.where(cb.equal(from.get(Company_.id), companyId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult().longValue() == 1);
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public Member findMemberByUserName(String userName) {
    try {/*  w  w w . jav a 2  s  . co m*/

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
        Root<Member> member = criteria.from(Member.class);

        criteria.select(member).where(builder.equal(member.get("name"), userName));
        return em.createQuery(criteria).getSingleResult();

    } catch (NoResultException e) {

        return new Member();
    }

}

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

public Page<Review> findPage(Member member, Product product, Type type, Boolean isShow, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }// ww w . j av  a  2  s  .co m
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    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.ReviewDaoImpl.java

public Long count(Member member, Product product, Type type, Boolean isShow) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }//from  ww w.ja v a2  s .  c  om
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    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.ReviewDaoImpl.java

public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class);
    Root<Review> root = criteriaQuery.from(Review.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }/*ww w.  ja  va  2s . com*/
    if (product != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
    }
    if (type == Type.positive) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4));
    } else if (type == Type.moderate) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3));
    } else if (type == Type.negative) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2));
    }
    if (isShow != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}