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.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);/*from   ww w.  jav  a2s .c  om*/
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }
    return super.findPage(criteriaQuery, pageable);
}

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

/**
 * {@inheritDoc}/*from  w w w. j  av a  2s  . c  o  m*/
 */
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:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}//w w  w .  j  a va2  s . co  m
 */
public long getCustomersCount(Map<String, Object> filters) {

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

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

protected T findLast() {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = em.createQuery(cq);

    List<T> option = Optional.ofNullable(q.setMaxResults(1).getResultList()).orElse(new ArrayList());

    T entity = null;// w  w  w  .j a v  a2s .  c o m
    if (Objects.nonNull(option.get(0))) {
        entity = option.get(0);
    }
    return entity;
}

From source file:com.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public List<E> findAll() {

    try {/*  w w w  . j  a  v a  2 s  . c  o m*/
        final CriteriaQuery<E> query = getEntityManager().getCriteriaBuilder().createQuery(entityType);
        final Root<E> root = query.from(entityType);
        final CriteriaQuery<E> rootQuery = query.select(root);
        final TypedQuery<E> allQuery = getEntityManager().createQuery(rootQuery);
        stopWatchThread.get().start();
        final List<E> rows = allQuery.getResultList();
        stopWatchThread.get().stop();
        LOG.info("Located: [{} rows] during findAll({}) in [{}ms]", rows.size(), entityType.getSimpleName(),
                stopWatchThread.get().getLastTaskTimeMillis());
        return rows;
    } finally {
        if (stopWatchThread.get().isRunning())
            stopWatchThread.get().stop();
    }
}

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

/**
 * {@inheritDoc}//from  w w  w  . j ava2  s .co  m
 */
public List<BulkUpload> getBulkUploads(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters) {

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

    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();

}

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

@Override
public boolean isValidUser(String userName, String password) {
    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))
            .where(builder.equal(member.get("password"), password));
    try {/*  w ww. ja v a2 s . c o  m*/

        Member validMember = em.createQuery(criteria).setMaxResults(1).getSingleResult();
        return ObjectUtil.verifyMemberExists(validMember);

    } catch (NoResultException e) {
        return false;
    }

}

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

/**
 * {@inheritDoc}//ww w.ja  va 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:com.devicehive.dao.rdbms.UserDaoRdbmsImpl.java

@Override
public List<UserVO> list(String login, String loginPattern, Integer role, Integer status, String sortField,
        Boolean sortOrderAsc, Integer take, Integer skip) {
    CriteriaBuilder cb = criteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> from = cq.from(User.class);

    Predicate[] predicates = CriteriaHelper.userListPredicates(cb, from, ofNullable(login),
            ofNullable(loginPattern), ofNullable(role), ofNullable(status));
    cq.where(predicates);/*from ww  w  . java  2s  .c  o  m*/
    CriteriaHelper.order(cb, cq, from, ofNullable(sortField), Boolean.TRUE.equals(sortOrderAsc));

    TypedQuery<User> query = createQuery(cq);
    cacheQuery(query, of(CacheConfig.refresh()));
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);
    return query.getResultList().stream().map(User::convertToVo).collect(Collectors.toList());
}

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

/**
 * {@inheritDoc}//from w  ww.j a  va2 s .co m
 */
public User findLastCustomer() {
    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));
    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 customer", e);
    }
    return null;
}