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: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:com.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public Collection<Board> boardListWithPaging(Board vo) {
    int bbsId = vo.getBbsId();
    int pageSize = vo.getPageSize();
    int pageNumber = (int) vo.getPageIndex();

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Board> criteriaQuery = criteriaBuilder.createQuery(Board.class);

    Root<Board> from = criteriaQuery.from(Board.class);
    CriteriaQuery<Board> select = criteriaQuery.select(from);
    if (bbsId > 0) {
        criteriaQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId));
    }//  w w  w  . j  a v  a 2 s  .  co  m
    /**list desc for date*/
    criteriaQuery.orderBy(criteriaBuilder.desc(from.get("frstRegistPnttm")));
    TypedQuery<Board> typedQuery = em.createQuery(select);

    typedQuery.setFirstResult((pageNumber - 1) * pageSize);
    typedQuery.setMaxResults(pageSize);

    Collection<Board> fooList = typedQuery.getResultList();

    return fooList;

}

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

/**
 * {@inheritDoc}/*from w w  w. j  av a  2s. com*/
 */
public long getCustomerCreditCardsCount(long userId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<CreditCard> root = query.from(CreditCard.class);
    query.select(builder.count(root));

    if (CommonUtil.isNotNull(userId)) {
        User user = userRepository.findById(userId);
        if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) {
            query.where(builder.equal(root.<Long>get(FIELDCONSTANT_CARDHOLDERID), user.getId()));
        }
    }
    return entityManager.createQuery(query).getSingleResult();
}

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

public Page<Member> findPage(Member.RankingType rankingType, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);/*from w  w  w  .  j a v a2  s  . c  o  m*/
    if (rankingType != null) {
        switch (rankingType) {
        case point:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("point")));
            break;
        case balance:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("balance")));
            break;
        case amount:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("amount")));
            break;
        }
    }
    return super.findPage(criteriaQuery, pageable);
}

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

public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) {
    Assert.notNull(period);//from   w w  w.  ja  v  a2  s  .  c om

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class);
    Root<Statistic> root = criteriaQuery.from(Statistic.class);
    switch (period) {
    case year:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"),
                criteriaBuilder.sum(root.<Long>get("registerMemberCount")),
                criteriaBuilder.sum(root.<Long>get("createOrderCount")),
                criteriaBuilder.sum(root.<Long>get("completeOrderCount")),
                criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")),
                criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount"))));
        criteriaQuery.groupBy(root.get("year"));
        break;
    case month:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"),
                criteriaBuilder.sum(root.<Long>get("registerMemberCount")),
                criteriaBuilder.sum(root.<Long>get("createOrderCount")),
                criteriaBuilder.sum(root.<Long>get("completeOrderCount")),
                criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")),
                criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount"))));
        criteriaQuery.groupBy(root.get("year"), root.get("month"));
        break;
    case day:
        criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"),
                root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"),
                root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"),
                root.<BigDecimal>get("completeOrderAmount")));
        break;
    }
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        Calendar calendar = DateUtils.toCalendar(beginDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.greaterThan(root.<Integer>get("month"), month)),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.equal(root.<Integer>get("month"), month),
                                criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day))));
    }
    if (endDate != null) {
        Calendar calendar = DateUtils.toCalendar(endDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.lessThan(root.<Integer>get("month"), month)),
                        criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year),
                                criteriaBuilder.equal(root.<Integer>get("month"), month),
                                criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day))));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).getResultList();
}

From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

@Override
public long getAllowedDeviceCount(HivePrincipal principal, List<String> guids) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<Device> criteria = cb.createQuery(Device.class);
    final Root<Device> from = criteria.from(Device.class);
    final Predicate[] predicates = CriteriaHelper.deviceListPredicates(cb, from, guids,
            Optional.ofNullable(principal));
    criteria.where(predicates);//from www  .  j av a2  s.c  om
    final TypedQuery<Device> query = createQuery(criteria);
    return query.getResultList().size();
}

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

/**
 * {@inheritDoc}/*  w  w w  .  j a  v  a  2s. 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:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

@Override
public List<DeviceVO> getDeviceList(List<String> guids, HivePrincipal principal) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<Device> criteria = cb.createQuery(Device.class);
    final Root<Device> from = criteria.from(Device.class);
    final Predicate[] predicates = CriteriaHelper.deviceListPredicates(cb, from, guids,
            Optional.ofNullable(principal));
    criteria.where(predicates);/*  ww  w  . ja  v  a  2s . c  om*/
    final TypedQuery<Device> query = createQuery(criteria);
    CacheHelper.cacheable(query);
    return query.getResultList().stream().map(Device::convertToVo).collect(Collectors.toList());
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultGenericDAO.java

@Override
public Long countAll() {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    cq.select(cb.count(cq.from(type)));//from w  ww  . j  a  v a  2 s.  c o m
    return getEntityManager().createQuery(cq).getSingleResult();
}

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

/**
 * {@inheritDoc}//w w  w.  j a va 2s .co m
 */
public List<User> getCustomers() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.select(root);
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    return entityManager.createQuery(query).getResultList();
}