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

public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);/* w w w . ja va  2 s.c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllPresentation = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PresentationImpl>>() {
                @Override/* ww  w  .  ja  va2  s . co  m*/
                public CriteriaQuery<PresentationImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PresentationImpl> criteriaQuery = cb
                            .createQuery(PresentationImpl.class);
                    final Root<PresentationImpl> definitionRoot = criteriaQuery.from(PresentationImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}

From source file:csns.model.core.dao.jpa.UserDaoImpl.java

@Override
public List<User> getUsers(Long ids[]) {
    if (ids == null || ids.length < 1)
        return new ArrayList<User>();

    CriteriaBuilder cbuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cquery = cbuilder.createQuery(User.class);
    Root<User> user = cquery.from(User.class);

    Predicate criteria = cbuilder.equal(user.get("id"), ids[0]);
    for (int i = 1; i < ids.length; ++i)
        criteria = cbuilder.or(criteria, cbuilder.equal(user.get("id"), ids[i]));
    cquery.where(criteria);/*from  w w  w .j  a  v a2s  .  co  m*/

    cquery.orderBy(cbuilder.asc(user.get("lastName")), cbuilder.asc(user.get("firstName")));

    return entityManager.createQuery(cquery).getResultList();
}

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

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {/* w w  w  .ja  v  a2s .c  o  m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class);
    Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (isMarketable != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable));
    }
    if (isOutOfStock != null) {
        Path<Integer> stock = root.get("product").get("stock");
        Path<Integer> allocatedStock = root.get("product").get("allocatedStock");
        if (isOutOfStock) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock),
                    criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock),
                    criteriaBuilder.greaterThan(stock, allocatedStock)));
        }
    }
    if (hasSent != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent));
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:dao.jpa.TestJpaDao.java

@Test
@Transactional/*from w  ww .  java  2  s  .  c om*/
public void testCountCriteria() {
    EntityManager em = bookDao.getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> criteria = cb.createQuery(Book.class);

    Root<Book> root = criteria.from(Book.class);
    Join<Book, Author> join = root.join("author");
    criteria.where(join.isNotNull());

    CriteriaQuery<Long> countCriteria = JpaUtils.countCriteria(em, criteria);
    Long result = em.createQuery(countCriteria).getSingleResult();
    log.debug("Count: " + result);
}

From source file:com.movies.dao.impl.BaseDaoImpl.java

@Override
public <T> List<T> getAllObjects(Class clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(clazz);
    Root<T> object = cq.from(clazz);
    cq.select(object);// w ww.  jav a  2  s.  co  m
    TypedQuery<T> q = em.createQuery(cq);
    return q.getResultList();
}

From source file:eu.uqasar.service.ProductService.java

/**
 * /*from  w w w .  ja  v a2s  .c om*/
 * @param productId
 * @return
 */
public boolean productExists(Long productId) {
    logger.info(String.format("checking if product with ID %d exists ...", productId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(cb.equal(from.get(Product_.id), productId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> sortAscendingDates() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.select(from);/*from w  w  w . j  a va2  s . com*/
    return em.createQuery(criteria.orderBy(cb.asc(from.get("releaseDate")))).getResultList();
}

From source file:eu.uqasar.service.ProductService.java

public List fillVersionDropDownChoice() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.multiselect(from.get(Product_.version));
    return (List) em.createQuery(criteria).getResultList();
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> sortDescendingDates() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.select(from);//www  . ja  va  2 s  . c  o m
    return em.createQuery(criteria.orderBy(cb.desc(from.get("releaseDate")))).getResultList();
}