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.carser.viamais.vo.TransactionFilter.java

public Long count(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.select(cb.countDistinct((transaction)));
    cq.where(getPredicates(cb, transaction));
    return em.createQuery(cq).getSingleResult();
}

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

@Test
@Transactional//from   w  w  w  .j a  va 2 s. c  om
public void testQuot() {
    NumericEntity entity2 = new NumericEntity();
    entity2.setLongField(2L);
    entity2.setIntField(3);
    entity2 = this.save(entity2);

    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<Number> criteriaQuery = builder.createQuery(Number.class);
    final Root<NumericEntity> root = criteriaQuery.from(NumericEntity.class);
    final Expression<Number> quot = builder.quot(
            builder.prod(builder.literal(1f), root.get(NumericEntity_.intField)),
            root.get(NumericEntity_.longField));
    criteriaQuery.select(quot);

    final Number result = this.findSingle(criteriaQuery);
    Assert.assertEquals(Float.class, result.getClass());
    Assert.assertEquals(3 / 2f, result);
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

@VisibleForTesting
int getNbEntriesBeforeSlicing(EntityManager entityManager, Class<E> entityClass,
        CriteriaBuilder criteriaBuilder, Predicate[] predicates) {

    CriteriaQuery<Long> counterQuery = criteriaBuilder.createQuery(Long.class);

    CriteriaQuery<Long> select = counterQuery.select(criteriaBuilder.count(counterQuery.from(entityClass)));

    if (predicates.length > 0) {
        select.where(predicates);//from   w  ww  . java  2s .co  m
    }

    return entityManager.createQuery(counterQuery).getSingleResult().intValue();
}

From source file:$.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. c o 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:$.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  www .  j a v a  2  s.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).orderBy(cb.asc(member.get("name")));
        return em.createQuery(criteria).getResultList();
    }

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private Product getProduct_Mercilessly() {
    return transactionTemplate.execute(new TransactionCallback<Product>() {
        @Override//from  w  ww . jav a  2 s  .  c  om
        public Product doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> productRoot = query.from(Product.class);

            query.select(productRoot)
                    .where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"), cb.gt(
                            productRoot.get(Product_.warehouseProductInfo).get(WarehouseProductInfo_.quantity),
                            50)));
            return entityManager.createQuery(query).getSingleResult();
        }
    });
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private List<Product> getProducts_Mercilessly() {
    return transactionTemplate.execute(new TransactionCallback<List<Product>>() {
        @Override// w  w w  .java 2 s.com
        public List<Product> doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> product = query.from(Product.class);
            query.select(product);
            query.distinct(true);

            List<Predicate> criteria = new ArrayList<Predicate>();
            criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%"));

            Subquery<Long> subQuery = query.subquery(Long.class);
            Root<Image> infoRoot = subQuery.from(Image.class);
            Join<Image, Product> productJoin = infoRoot.join(Image_.product);
            subQuery.select(productJoin.<Long>get(Product_.id));

            subQuery.where(cb.gt(infoRoot.get(Image_.index), 0));
            criteria.add(cb.in(product.get(Product_.id)).value(subQuery));
            query.where(cb.and(criteria.toArray(new Predicate[criteria.size()])));
            return entityManager.createQuery(query).getResultList();
        }
    });
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.UserSessionUrlDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllUserSessionUrl = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<UserSessionUrlImpl>>() {
                @Override/*from ww  w  .jav  a 2  s . c  o  m*/
                public CriteriaQuery<UserSessionUrlImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<UserSessionUrlImpl> criteriaQuery = cb
                            .createQuery(UserSessionUrlImpl.class);
                    final Root<UserSessionUrlImpl> definitionRoot = criteriaQuery
                            .from(UserSessionUrlImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });

}

From source file:com.sfs.captor.controller.ProductReleaseAction.java

/**
 * load product releases//from ww w.j  a va2s.  c  o m
 */
private void loadList() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ProductRelease> c = cb.createQuery(ProductRelease.class);
    Root<ProductRelease> obj = c.from(ProductRelease.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.productReleases = em.createQuery(c).getResultList();
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * list all entity// ww  w.ja v  a 2 s . c o m
 *
 * @return
 */
public List<T> findAll() {
    EntityManager em = getEntityManager();
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return em.createQuery(cq).getResultList();
}