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:net.groupbuy.dao.impl.OrderDaoImpl.java

public Page<Order> findPage(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }//from w  w w . j ava  2 s .  com
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

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

public Long count(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired) {//  w ww .ja  v  a 2  s  .  co  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java

@Override
public OpenHubProjectEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubProjectEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubProjectEntity> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.select(root);

    Predicate predicate = criteriaBuilder.equal(root.get("name"), name);
    criteriaQuery.where(predicate);/*from  w w w .j  av  a 2s . co  m*/

    TypedQuery<OpenHubProjectEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubProjectEntity projectEntity = query.getSingleResult();

    return projectEntity;
}

From source file:br.com.compositeam.data.MemberDaoImpl.java

public Member findByEmail(String email) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*/*  w w w  .  j a  v a 2  s.co  m*/
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

    criteria.select(member).where(cb.equal(member.get("email"), email));
    return em.createQuery(criteria).getSingleResult();
}

From source file:br.com.compositeam.data.MemberDaoImpl.java

public List<Member> findAllOrderedByName() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*/*from   ww  w.j  av  a2 s.  co m*/
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}

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

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public <E extends Entity<K>, K> long getCount(final Class<E> clazz) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
    final Root<E> from = criteriaQuery.from(clazz);

    final Expression<Long> count = builder.count(from);

    criteriaQuery.select(count);

    return this.findSingle(criteriaQuery).longValue();
}

From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java

/**
 * Returns a unsorted list of distinct values of the requested field, filtered using a {@link QueryCriteria}
 * based query.//from  w  w  w  .j ava  2 s.  c  o m
 *
 * @param field          Model field name.
 * @param queryCriterias Query criteria to filter the field values by.
 * @return Sorted list of distinct values of {@code field}.
 */
public Iterable<Object> distinct(String field, Iterable<QueryCriteria> queryCriterias) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType());
    Root<T> root = query.from(metadata.getJavaType());
    query.where(queryBuilder.createSpecification(queryCriterias).toPredicate(root, query, builder));
    query.select(root.get(field)).distinct(true);
    return (List) entityManager.createQuery(query).getResultList();
}

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

@Override
public Account fetchAccountWithPrincipalName(String principalName) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    try {//from  w  w  w  . j av a2s.c o  m
        return entityManager
                .createQuery(
                        q.select(account).where(cb.equal(account.get(Account_.principalName), principalName)))
                .getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.jboss.spring.quickstarts.kitchensink.kitchensink_spring.repo.MemberDaoImpl.java

public Member findByEmail(String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    /*//from w w  w. j  av a2s.c  om
     * Swap criteria statements if you would like to try out type-safe criteria queries, a new
     * feature in JPA 2.0 criteria.select(member).orderBy(cb.asc(member.get(Member_.name)));
     */

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

From source file:org.apereo.portal.i18n.dao.jpa.JpaMessageDao.java

@Override
public void afterPropertiesSet() throws Exception {
    this.codeParameter = this.createParameterExpression(String.class, "code");
    this.localeParameter = this.createParameterExpression(Locale.class, "locale");

    this.findMessageByCodeAndLocaleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override/*from w w  w. jav  a 2  s. c o  m*/
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.and(cb.equal(root.get(MessageImpl_.code), codeParameter),
                            cb.equal(root.get(MessageImpl_.locale), localeParameter)));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.code)),
                            cb.asc(root.get(MessageImpl_.locale)));

                    return criteriaQuery;
                }
            });

    this.findMessageByCodeQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.equal(root.get(MessageImpl_.code), codeParameter));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.locale)));

                    return criteriaQuery;
                }
            });

    this.findMessageByLocaleQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MessageImpl>>() {
                @Override
                public CriteriaQuery<MessageImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<MessageImpl> criteriaQuery = cb.createQuery(MessageImpl.class);
                    final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(cb.equal(root.get(MessageImpl_.locale), localeParameter));
                    criteriaQuery.orderBy(cb.asc(root.get(MessageImpl_.code)));

                    return criteriaQuery;
                }
            });

    this.findCodes = this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<String>>() {
        @Override
        public CriteriaQuery<String> apply(CriteriaBuilder cb) {
            final CriteriaQuery<String> criteriaQuery = cb.createQuery(String.class);
            final Root<MessageImpl> root = criteriaQuery.from(MessageImpl.class);
            criteriaQuery.select(root.get(MessageImpl_.code));
            criteriaQuery.groupBy(root.get(MessageImpl_.code));
            return criteriaQuery;
        }
    });
}