Example usage for javax.persistence.criteria CriteriaQuery where

List of usage examples for javax.persistence.criteria CriteriaQuery where

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery where.

Prototype

CriteriaQuery<T> where(Predicate... restrictions);

Source Link

Document

Modify the query to restrict the query result according to the conjunction of the specified restriction predicates.

Usage

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

public Long waitingPaymentCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/*from  w  ww .ja v a 2  s . c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled));
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.or(criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.unpaid),
                    criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.partialPayment)));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters,
        List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class);
    Root<Promotion> root = criteriaQuery.from(Promotion.class);
    criteriaQuery.select(root);/*from  w w w. j a  v a  2s.c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (hasBegun != null) {
        if (hasBegun) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(),
                    criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date())));
        } else {
            restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(),
                    criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date()));
        }
    }
    if (hasEnded != null) {
        if (hasEnded) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:org.finra.herd.dao.impl.BusinessObjectDefinitionDaoImpl.java

@Override
public List<BusinessObjectDefinitionKey> getBusinessObjectDefinitionKeysByNamespace(String namespaceCode) {
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object definition.
    Root<BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = criteria
            .from(BusinessObjectDefinitionEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);

    // Get the columns.
    Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code);
    Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntity
            .get(BusinessObjectDefinitionEntity_.name);

    // Add the select clause.
    criteria.multiselect(namespaceCodeColumn, businessObjectDefinitionNameColumn);

    // If namespace code is specified, add the where clause.
    if (StringUtils.isNotBlank(namespaceCode)) {
        criteria.where(builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)),
                namespaceCode.toUpperCase()));
    }//from w ww.ja  v a2 s .  c  om

    // Add the order by clause.
    if (StringUtils.isNotBlank(namespaceCode)) {
        criteria.orderBy(builder.asc(businessObjectDefinitionNameColumn));
    } else {
        criteria.orderBy(builder.asc(businessObjectDefinitionNameColumn), builder.asc(namespaceCodeColumn));
    }

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();

    // Populate the "keys" objects from the returned tuples (i.e. 1 tuple for each row).
    List<BusinessObjectDefinitionKey> businessObjectDefinitionKeys = new ArrayList<>();
    for (Tuple tuple : tuples) {
        BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey();
        businessObjectDefinitionKeys.add(businessObjectDefinitionKey);
        businessObjectDefinitionKey.setNamespace(tuple.get(namespaceCodeColumn));
        businessObjectDefinitionKey
                .setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNameColumn));
    }

    return businessObjectDefinitionKeys;
}

From source file:com.yunguchang.data.ApplicationRepository.java

public TBusApplyinfoEntity getApplicationById(String id) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    cq.where(cb.equal(applyRoot.get(TBusApplyinfoEntity_.uuid), id));
    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:com.yunguchang.data.ApplicationRepository.java

public TBusApplyinfoEntity getApplicationByNo(String applyNo, PrincipalExt principalExtOrNull) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> scheduleRoot = cq.from(TBusApplyinfoEntity.class);
    cq.where(cb.equal(scheduleRoot.get(TBusApplyinfoEntity_.applyno), applyNo));
    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:com.yunguchang.data.ApplicationRepository.java

@TransactionAttribute(REQUIRES_NEW)
public void deleteSchedule(String scheduleId, PrincipalExt principalExt) {
    applySecurityFilter("applications", principalExt);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusScheduleRelaEntity> cq = cb.createQuery(TBusScheduleRelaEntity.class);
    Root<TBusScheduleRelaEntity> scheduleRoot = cq.from(TBusScheduleRelaEntity.class);
    cq.where(cb.equal(scheduleRoot.get(TBusScheduleRelaEntity_.uuid), scheduleId));
    TBusScheduleRelaEntity relaEntity = Iterables.getFirst(em.createQuery(cq).getResultList(), null);

    for (TBusApplyinfoEntity applyinfoEntity : relaEntity.getApplications()) {
        TBusApplyinfoEntity applicationEntity = getApplicationById(applyinfoEntity.getUuid(), principalExt);
        applicationEntity.setSchedule(null);
    }/*from   ww w  .j  av a2  s .co  m*/
    relaEntity.setApplications(null);
    for (TBusScheduleCarEntity carEntity : relaEntity.getScheduleCars()) {
        carEntity = em.find(TBusScheduleCarEntity.class, carEntity.getUuid());
        em.remove(carEntity);
    }
    relaEntity.setScheduleCars(null);
    em.remove(relaEntity);
}

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

public List<Article> findList(ArticleCategory articleCategory, Date beginDate, Date endDate, Integer first,
        Integer count) {//from ww  w . ja  va 2  s.  c  o  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:com.yunguchang.data.ApplicationRepository.java

public TBusApproveSugEntity getApplyApproveInfoByApplyNo(String applyNo) {
    if (applyNo == null) {
        throw new InvalidParameterException("Apply No can not be null!");
    }//from  w  w  w  .  j a v a  2  s. co m
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class);
    Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class);
    approveRoot.fetch(TBusApproveSugEntity_.application);
    cq.where(cb.equal(approveRoot.get(TBusApproveSugEntity_.application).get(TBusApplyinfoEntity_.applyno),
            applyNo));
    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

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);/*ww w. ja  v  a 2 s  . 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.findPage(criteriaQuery, pageable);
}

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);//w  w  w  .j  a va 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);
}