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.navasoft.madcoin.backend.model.controller.impl.WorkRequestDataAccess.java

/**
 * Gets the rows by criteria.//from   w  ww.jav  a  2 s .  c  o  m
 * 
 * @return the rows by criteria
 * @since 9/09/2014, 11:22:17 AM
 */
public List<WorkRequests> getRowsByCriteria() {
    CriteriaQuery<WorkRequests> cq = entityManager.getCriteriaBuilder().createQuery(WorkRequests.class);
    Root<WorkRequests> rt = cq.from(WorkRequests.class);
    cq.select(rt);
    ParameterExpression<String> status = entityManager.getCriteriaBuilder().parameter(String.class);
    cq.where(entityManager.getCriteriaBuilder().equal(rt.get("processStatus"), status));
    TypedQuery<WorkRequests> q = entityManager.createQuery(cq);
    return q.getResultList();
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * /*from   w w w.j  a  v  a 2s . c om*/
 * @param user_level
 * @return
 */
public List<Organisation> getOrganisations(int start, int max, String orderby, boolean asc) {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Organisation> cq = cb.createQuery(Organisation.class);
        Root<Organisation> c = cq.from(Organisation.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Organisation> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Organisation> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getOrganisations]", ex2);
    }
    return null;
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Iterable<Account> findAccounts(boolean onlyAdmin, String query) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);
    Root<Account> account = q.from(Account.class);

    Expression<Boolean> expression = null;

    if (onlyAdmin) {
        // This looks like it could be replaced with account.get(Account_.admin), but it can't because of syntax
        expression = cb.equal(account.get(Account_.admin), true);
    }/*from  w w w.  j  a va  2 s .  c  o m*/

    if (query != null) {
        Expression<Boolean> queryExpression = cb.like(cb.lower(account.get(Account_.name)),
                "%" + query.toLowerCase() + "%");

        if (expression == null) {
            expression = queryExpression;
        } else {
            expression = cb.and(expression, queryExpression);
        }
    }

    if (expression != null) {
        q.where(expression);
    }

    return entityManager.createQuery(q.select(account)).getResultList();
}

From source file:de.tudarmstadt.ukp.csniper.webapp.security.dao.AbstractDao.java

/**
 * Finds all entities that have the same type as the given example and all fields are equal to
 * non-null fields in the example.//from  w  w w  .  j a va  2 s  . c o  m
 */
protected <TT> CriteriaQuery<TT> queryByExample(TT aExample, String aOrderBy, boolean aAscending) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    @SuppressWarnings("unchecked")
    CriteriaQuery<TT> query = cb.createQuery((Class<TT>) aExample.getClass());
    @SuppressWarnings("unchecked")
    Root<TT> root = query.from((Class<TT>) aExample.getClass());
    query.select(root);

    List<Predicate> predicates = new ArrayList<Predicate>();
    BeanWrapper a = PropertyAccessorFactory.forBeanPropertyAccess(aExample);

    // Iterate over all properties
    for (PropertyDescriptor d : a.getPropertyDescriptors()) {
        Object value = a.getPropertyValue(d.getName());

        // Only consider writeable properties. This filters out e.g. the "class" (getClass())
        // property.
        if (value != null && a.isWritableProperty(d.getName())) {
            predicates.add(cb.equal(root.get(d.getName()), value));
        }
    }

    if (!predicates.isEmpty()) {
        query.where(predicates.toArray(new Predicate[predicates.size()]));
    }

    if (aOrderBy != null) {
        if (aAscending) {
            query.orderBy(cb.asc(root.get(aOrderBy)));
        } else {
            query.orderBy(cb.desc(root.get(aOrderBy)));
        }
    }

    return query;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

public List<Organisation> getOrganisations(Long user_level) {
    try {/*from  w w  w  . j a v a2  s  .  com*/
        if (authLevelManagement.checkAdminLevel(user_level)) {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Organisation> cq = cb.createQuery(Organisation.class);
            Root<Organisation> c = cq.from(Organisation.class);
            Predicate condition = cb.equal(c.get("deleted"), "false");
            cq.where(condition);
            TypedQuery<Organisation> q = em.createQuery(cq);
            List<Organisation> ll = q.getResultList();
            return ll;
        } else {
            log.error("[getOrganisations] noPremission");
        }
    } catch (Exception ex2) {
        log.error("[getOrganisations]", ex2);
    }
    return null;
}

From source file:org.easy.criteria.CriteriaProcessor.java

/**
 * Finds all entities that satisfied the given criteria. This ignores the
 * "Select" clause. If you need to selected some specific columns then use
 * "findAllTuple" API.//from   w  w w  . j a  v a  2s .  co m
 * 
 * @param criteria
 *            - A restriction criteria or NULL to get every thing.
 * @param distinct
 * @param startIndex
 *            - Pass 0 or less to disable paging.
 * @param maxResult
 *            - Pass 0 or less to disable paging.
 * @param lockMode
 *            - Pass NULL if your are not managing transaction.
 *            LockModeType.NONE will through exception if no transaction is
 *            active.
 * @return - A list of entities or an empty list if no result were found.
 */
public <T> List<T> findAllEntity(CriteriaComposer<T> criteria, boolean distinct, QueryProperties properties) {
    log.trace("CriteriaProcessor.findAllEntity");

    Preconditions.checkNotNull(criteria);

    Class<T> forClass = criteria.getEntityClass();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(forClass);

    log.debug("root =" + forClass.getName());

    Root<T> root = criteriaQuery.from(forClass);

    criteriaQuery.distinct(distinct);

    List<Predicate> wherePredicates = new ArrayList<Predicate>();

    Map<Integer, Order> orderBy = new HashMap<Integer, Order>(0);

    if (criteria != null) {
        criteria.generateJoins(root);
        criteria.generateWhere(criteriaBuilder, wherePredicates);
        criteria.generateOrderBy(criteriaBuilder, orderBy);
    }

    criteriaQuery.where(wherePredicates.toArray(new Predicate[wherePredicates.size()]));

    // Order by
    if (orderBy != null && orderBy.size() > 0) {
        Order[] orderByList = new Order[orderBy.size()];
        orderBy.values().toArray(orderByList);
        criteriaQuery.orderBy(orderByList);
    }

    TypedQuery<T> query = entityManager.createQuery(criteriaQuery);

    if (properties != null)
        properties.applyProperties(query);

    List<T> result = query.getResultList();

    if (result == null)
        result = new ArrayList<T>(0);

    log.debug("CriteriaProcessor.findAllEntity result size=" + result.size());

    return result;
}

From source file:com.ims.service.OrderService.java

public List<PurchaseOrder> searchPurchaseOrderList(final OrderSearchCriteria orderSearchCriteria) {
    List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();
    Specification<PurchaseOrder> speci = new Specification<PurchaseOrder>() {
        @Override/*from ww w  . jav a2 s . com*/
        public Predicate toPredicate(Root<PurchaseOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<Predicate>();
            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderStatus())) {
                predicates.add(cb.equal(root.get("status"), orderSearchCriteria.getPurchaseOrderStatus()));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderNum())) {
                predicates.add(cb.like(root.<String>get("purchaseOrderNumber"),
                        "%" + orderSearchCriteria.getPurchaseOrderNum() + "%"));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getProformaInvoiceNum())) {
                predicates.add(cb.like(root.<String>get("piNumber"),
                        "%" + orderSearchCriteria.getProformaInvoiceNum() + "%"));
            }

            query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])))
                    .orderBy(cb.desc(root.get("changeLog").get("createdDate")));
            return null;
        }
    };

    return purchaseOrderRepository.findAll(speci);
}

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);//w  w w  . ja va2 s  .  c o m
    Predicate restrictions = criteriaBuilder.conjunction();
    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);
    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);/*from w w w  .java 2  s.co  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate));
    }
    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.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java

@Override
public StaticAsset readStaticAssetByFullUrl(String fullUrl) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class);
    Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class);
    criteria.select(handler);/*w  ww.  ja  v a2  s .co m*/

    List<Predicate> restrictions = new ArrayList<Predicate>();
    List<Order> sorts = new ArrayList<Order>();
    restrictions.add(builder.equal(handler.get("fullUrl"), fullUrl));
    try {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null);
            queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria,
                    handler, restrictions);
            queryExtensionManager.getProxy().refineOrder(StaticAssetImpl.class, null, builder, criteria,
                    handler, sorts);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
        if (!org.apache.commons.collections.CollectionUtils.isEmpty(sorts)) {
            criteria.orderBy(sorts);
        }

        TypedQuery<StaticAsset> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List<StaticAsset> response = query.getResultList();
        if (response.size() > 0) {
            return response.get(0);
        }
        return null;
    } finally {
        if (queryExtensionManager != null) {
            queryExtensionManager.getProxy().breakdown(StaticAssetImpl.class, null);
        }
    }
}