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.shopxx.dao.impl.OrderDaoImpl.java

public Long createOrderCount(Date beginDate, Date endDate) {
    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 (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }/*from w w w .  ja  v a 2  s .c o  m*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

public Long completeOrderCount(Date beginDate, Date endDate) {
    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 (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate));
    }//from   w w  w  . j a v  a2 s . co m
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:org.apereo.portal.fragment.subscribe.dao.jpa.JpaUserFragmentSubscriptionDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.userIdParameter = this.createParameterExpression(Integer.TYPE, "userId");
    this.fragmentOwnerParameter = this.createParameterExpression(String.class, "fragmentOwner");

    this.findUserFragmentInfoByPersonQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<UserFragmentSubscriptionImpl>>() {
                @Override/*w w  w. j a  v a 2 s  . co m*/
                public CriteriaQuery<UserFragmentSubscriptionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<UserFragmentSubscriptionImpl> criteriaQuery = cb
                            .createQuery(UserFragmentSubscriptionImpl.class);
                    final Root<UserFragmentSubscriptionImpl> root = criteriaQuery
                            .from(UserFragmentSubscriptionImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery
                            .where(cb.equal(root.get(UserFragmentSubscriptionImpl_.userId), userIdParameter));

                    return criteriaQuery;
                }
            });

    this.findUserFragmentInfoByPersonAndOwnerQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<UserFragmentSubscriptionImpl>>() {
                @Override
                public CriteriaQuery<UserFragmentSubscriptionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<UserFragmentSubscriptionImpl> criteriaQuery = cb
                            .createQuery(UserFragmentSubscriptionImpl.class);
                    final Root<UserFragmentSubscriptionImpl> root = criteriaQuery
                            .from(UserFragmentSubscriptionImpl.class);
                    criteriaQuery.select(root);
                    criteriaQuery.where(
                            cb.and(cb.equal(root.get(UserFragmentSubscriptionImpl_.userId), userIdParameter),
                                    cb.equal(root.get(UserFragmentSubscriptionImpl_.fragmentOwner),
                                            fragmentOwnerParameter)));

                    return criteriaQuery;
                }
            });

    this.findUsersWithActiveSubscriptionsQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<String>>() {
                @Override
                public CriteriaQuery<String> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<String> criteriaQuery = cb.createQuery(String.class);
                    final Root<UserFragmentSubscriptionImpl> root = criteriaQuery
                            .from(UserFragmentSubscriptionImpl.class);
                    criteriaQuery.select(root.get(UserFragmentSubscriptionImpl_.createdBy));
                    criteriaQuery.where(cb.equal(root.get(UserFragmentSubscriptionImpl_.active), true));

                    return criteriaQuery;
                }
            });
}

From source file:com.sfs.ucm.controller.HelpContentAction.java

/**
 * load resources//w  ww . ja  va  2s. c om
 * 
 * @throws UCMException
 */
private void loadList() throws UCMException {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Help> c = cb.createQuery(Help.class);
        Root<Help> obj = c.from(Help.class);
        c.select(obj).orderBy(cb.asc(obj.get("keyword")));
        this.helpItems = em.createQuery(c).getResultList();

        this.itemCount = this.helpItems.size();
    } catch (Exception e) {
        throw new UCMException(e);
    }
}

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

public BigDecimal createOrderAmount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }/*from   ww  w. j a  va2s . c om*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult();
    return result != null ? result : BigDecimal.ZERO;
}

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

public BigDecimal completeOrderAmount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate));
    }/*ww  w  . jav a 2 s .c o  m*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult();
    return result != null ? result : BigDecimal.ZERO;
}

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

/**
 * Retrieves the extension with the given URN from the database
 *
 * @param urn/*from   w w  w. j  ava2  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:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}//from w  w w  .  j  a v  a2 s .c om
 */
public List<User> getCustomers(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate isCustomerRole = builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER);
    Predicate hasCreditCard = builder.equal(root.get(FIELD_CONSTANT_HAS_CREDIT_CARD), TRUE);
    query.where(builder.and(isCustomerRole, hasCreditCard));

    query.select(root);

    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(filters, builder, root, query);
    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();
}

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

/**
 * list entity by CriteriaInfo/*from  w ww.  j  av  a2  s. c  o m*/
 *
 * @param criteriaInfo
 * @return PagedResult
 */
public PagedResult<T> list(CriteriaInfo criteriaInfo) {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(entityClass);
    Root<T> userRoot = cq.from(entityClass);
    cq.select(userRoot);
    ParamInfo paramInfo = criteriaInfo.getParamInfo();
    PageInfo pageInfo = criteriaInfo.getPageInfo();
    SortInfo sortInfo = criteriaInfo.getSortInfo();

    //build query for paramInfo
    if (paramInfo != null) {
        Set<Predicate> andCriteria = new HashSet();
        Set<Predicate> orCriteria = new HashSet();

        for (ParamItem item : paramInfo.getParamItems()) {
            Predicate predicate;
            if (item.getValue() instanceof String) {
                //fuzy search for string
                String regExp = "%" + item.getValue() + "%";
                predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp);
            } else {
                predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue());
            }

            switch (item.getOperator()) {
            case AND:
                andCriteria.add(predicate);
                break;
            case OR:
                orCriteria.add(predicate);
                break;
            }
        }
        if (orCriteria.size() > 0) {
            Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()]));
            andCriteria.add(or);
        }
        if (andCriteria.size() > 0) {
            Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()]));
            cq.where(and);
        }
    }

    //build query for sortInfo
    Set<Order> orderPredicate = new HashSet<>();
    if (sortInfo != null) {
        for (SortItem item : sortInfo.getSortItems()) {
            if (item.isDescending()) {
                orderPredicate.add(cb.desc(userRoot.get(item.getFieldName())));
            } else {
                orderPredicate.add(cb.asc(userRoot.get(item.getFieldName())));
            }
        }
    }
    if (orderPredicate.size() > 0) {
        cq.orderBy(orderPredicate.toArray(new Order[orderPredicate.size()]));
    }

    TypedQuery<T> query = em.createQuery(cq);
    //set result range
    if (pageInfo != null) {
        query.setFirstResult(pageInfo.getOffset());
        query.setMaxResults(pageInfo.getSize());
    }

    int totalSize;
    if (paramInfo != null && paramInfo.getParamItems().size() > 0) {
        totalSize = count(paramInfo);
    } else {
        totalSize = count();
    }

    return new PagedResult(query.getResultList(), totalSize);
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public int itemsDateRangeCount(Date start, Date end) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.select(cb.count(rt.get(ActivityAIPC_.actinstproccodeser)));
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();

}