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.ArticleDaoImpl.java

public Page<Article> findPage(ArticleCategory articleCategory, List<Tag> tags, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);/*w  w w.j ava 2s. co  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (tags != null && !tags.isEmpty()) {
        Subquery<Article> subquery = criteriaQuery.subquery(Article.class);
        Root<Article> subqueryRoot = subquery.from(Article.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findPage(criteriaQuery, pageable);
}

From source file:org.osiam.resource_server.storage.dao.ExtensionDao.java

/**
 * Retrieves the extension with the given URN from the database
 *
 * @param urn//from   ww w  .ja v a 2  s .c  o m
 *        the URN of the extension to look up
 * @param caseInsensitive
 *        should the case of the URN be ignored
 * @return the extension entity
 */
public ExtensionEntity getExtensionByUrn(String urn, boolean caseInsensitive) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ExtensionEntity> cq = cb.createQuery(ExtensionEntity.class);
    Root<ExtensionEntity> extension = cq.from(ExtensionEntity.class);

    Predicate predicate;
    if (caseInsensitive) {
        predicate = cb.equal(cb.lower(extension.get(ExtensionEntity_.urn)), urn.toLowerCase(Locale.ENGLISH));
    } else {
        predicate = cb.equal(extension.get(ExtensionEntity_.urn), urn);
    }

    cq.select(extension).where(predicate);

    TypedQuery<ExtensionEntity> query = em.createQuery(cq);

    ExtensionEntity singleExtension;

    try {
        singleExtension = query.getSingleResult();
    } catch (NoResultException e) {
        throw new OsiamException("Could not find the Extension '" + urn + "'.", e);
    }

    return singleExtension;
}

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

public List<Article> findList(ArticleCategory articleCategory, List<Tag> tags, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);//from  w w  w. ja  v a 2s  .  c  om
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (tags != null && !tags.isEmpty()) {
        Subquery<Article> subquery = criteriaQuery.subquery(Article.class);
        Root<Article> subqueryRoot = subquery.from(Article.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

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

public List<Article> findList(ArticleCategory articleCategory, Date beginDate, Date endDate, Integer first,
        Integer count) {//from   w w w .  j  a  va 2s.co  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, first, count, null, null);
}

From source file:controllers.AuthController.java

@UnitOfWork
@FilterWith(CTCheck.class)
public Result login(Context context, Session session, User req) {
    EntityManager entitymanager = entitiyManagerProvider.get();
    CriteriaBuilder cb = entitymanager.getCriteriaBuilder();

    try {/*from   w  w w.  j ava 2 s. c o m*/
        CriteriaQuery<User> query = cb.createQuery(User.class);
        Root<User> a = query.from(User.class);
        query.where(cb.equal(a.get(User_.email), req.getEmail()),
                cb.equal(a.get(User_.password), req.getPassword()));
        User u = entitymanager.createQuery(query).getSingleResult();
        String token = SessionIdentifierGenerator.nextSessionId();
        context.addCookie(Cookie.builder("token", token).build());
        session.put("token", token);
        session.put("email", u.getEmail());
        session.put("id", String.valueOf(u.getId()));
        ninjaCache.set(token, u.getId());
        return Results.json().render(new RespAuth(u.getId(), token, u.getEmail()));
    } catch (Exception e) {
        e.printStackTrace();
        return Results.json().status(400).render(new JSendResp(400, e));
    }
}

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

public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Member> member = criteriaQuery.from(Member.class);
    Join<Product, Order> orders = member.join("orders");
    criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"),
            member.get("point"), member.get("amount"), member.get("balance"),
            criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate));
    }/*from  w w  w.ja  va  2 s  .  c  om*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"),
            member.get("amount"), member.get("balance"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:dao.jpa.TestJpaDao.java

@Test
@Transactional//from w w  w  .  j  av  a 2s .c  o  m
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.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * {@inheritDoc}/*  w  w  w . ja  va2s .  co m*/
 */
public long getBulkUploadsCount(Map<String, Object> filters) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(builder.count(root));
    addFilterCriteria(filters, builder, root, query);

    return entityManager.createQuery(query).getSingleResult();
}

From source file:net.shopxx.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  .  j  a  v a 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("goods").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:net.shopxx.dao.impl.ProductNotifyDaoImpl.java

public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent,
        Pageable pageable) {//ww  w .j  av  a  2 s .  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("goods").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);
}