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.dontdrinkandroot.persistence.dao.TypedJpaDao.java

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public List<E> findAll(final Collection<PredicateBuilder<E>> filters) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    final Predicate[] predicates = new Predicate[filters.size()];
    int count = 0;
    for (final PredicateBuilder<E> filter : filters) {
        predicates[count] = filter.createPredicate(builder, from);
        count++;//from  w  w w.  j  a  va2 s  . c  o m
    }

    criteriaQuery.where(predicates);

    return this.find(criteriaQuery);
}

From source file:net.dontdrinkandroot.persistence.dao.TypedJpaDao.java

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public List<E> findAll(final PredicateBuilder<E>... filters) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<E> criteriaQuery = builder.createQuery(this.entityClass);
    final Root<E> from = criteriaQuery.from(this.entityClass);

    final Predicate[] predicates = new Predicate[filters.length];
    int count = 0;
    for (final PredicateBuilder<E> filter : filters) {
        predicates[count] = filter.createPredicate(builder, from);
        count++;//from  w w w  .java2s .c  o  m
    }

    criteriaQuery.where(predicates);

    return this.find(criteriaQuery);
}

From source file:eu.uqasar.service.user.UserService.java

public long countAllFiltered(final UserFilterStructure filter) {
    logger.infof("counting all Users matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<User> from = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from   w  w w .jav  a2s  .c o  m
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllByAscendingNameFiltered(UserFilterStructure filter, int first, int count) {
    logger.infof("loading all Users ordered by ascending name matching the given filter %s...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> root = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from  ww  w. j ava2 s  .  c  o  m*/
    criteria.orderBy(cb.asc(root.get(User_.lastName)), cb.asc(root.get(User_.firstName)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

/**
 * Find all entities of the given type that are associated with this parent.
 * @param childClass// ww  w  .  j ava 2  s .c om
 * @param parent
 * @return
 */
public <A, E> List<A> findByAssociation(Class<A> forClass, SingularAttribute<A, E> associationAttribute,
        E associatedWith) {

    if (log.isTraceEnabled())
        log.trace("findAssociatedEntity: ");

    CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder();
    CriteriaQuery<A> criteria = criteriaBuilder.createQuery(forClass);

    log.debug("Root :" + forClass.getSimpleName());
    Root<A> child = criteria.from(forClass);

    criteria.distinct(true);

    criteria.where(criteriaBuilder.equal(child.get(associationAttribute), associatedWith));

    TypedQuery<A> query = _entityManager.createQuery(criteria);

    return query.getResultList();
}

From source file:org.apache.ranger.service.XTrxLogService.java

@Override
public VXTrxLogList searchXTrxLogs(SearchCriteria searchCriteria) {
    EntityManager em = daoMgr.getEntityManager();
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(VXXTrxLog.class);
    Root<VXXTrxLog> rootEntityType = selectCQ.from(VXXTrxLog.class);
    Predicate predicate = generatePredicate(searchCriteria, em, criteriaBuilder, rootEntityType);

    selectCQ.where(predicate);
    if ("asc".equalsIgnoreCase(searchCriteria.getSortType())) {
        selectCQ.orderBy(criteriaBuilder.asc(rootEntityType.get("createTime")));
    } else {//from   ww  w .  ja  va2 s .c om
        selectCQ.orderBy(criteriaBuilder.desc(rootEntityType.get("createTime")));
    }
    int startIndex = searchCriteria.getStartIndex();
    int pageSize = searchCriteria.getMaxRows();
    List<VXXTrxLog> resultList = em.createQuery(selectCQ).setFirstResult(startIndex).setMaxResults(pageSize)
            .getResultList();

    List<VXTrxLog> trxLogList = new ArrayList<VXTrxLog>();
    for (VXXTrxLog xTrxLog : resultList) {
        VXTrxLog trxLog = mapCustomViewToViewObj(xTrxLog);

        if (trxLog.getUpdatedBy() != null) {
            XXPortalUser xXPortalUser = rangerDaoManager.getXXPortalUser()
                    .getById(Long.parseLong(trxLog.getUpdatedBy()));
            if (xXPortalUser != null) {
                trxLog.setOwner(xXPortalUser.getLoginId());
            }
        }

        trxLogList.add(trxLog);
    }

    VXTrxLogList vxTrxLogList = new VXTrxLogList();
    vxTrxLogList.setStartIndex(startIndex);
    vxTrxLogList.setPageSize(pageSize);
    vxTrxLogList.setVXTrxLogs(trxLogList);
    return vxTrxLogList;
}

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 ww.j  a v a  2 s.  c  o 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:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java

@Override
public List<HarvesterSetting> findChildrenByName(int parentid, String name) {
    CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder();
    CriteriaQuery<HarvesterSetting> query = criteriaBuilder.createQuery(HarvesterSetting.class);

    Root<HarvesterSetting> root = query.from(HarvesterSetting.class);
    Predicate equalParentId = criteriaBuilder.equal(root.get(HarvesterSetting_.parent), parentid);
    Predicate equalName = criteriaBuilder.equal(root.get(HarvesterSetting_.name), name);
    query.where(criteriaBuilder.and(equalParentId, equalName));

    return _entityManager.createQuery(query).getResultList();
}

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

protected <T extends Persistable> List<T> getAllOrderedExcept(Class<T> clazz, Collection<T> toExclude,
        Order... orders) {/*from ww w .j  a v  a 2 s .  c  om*/
    if (toExclude == null || toExclude.isEmpty()) {
        return getAll(clazz);
    }
    logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    if (orders != null && orders.length > 0) {
        query.orderBy(orders);
    }
    query.where(cb.not(root.in(toExclude)));
    return em.createQuery(query).getResultList();
}

From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

/**
 * Retrieves the distinct UUIDs of the available requesting facilities.
 *//*from  w w w .j a  v a  2  s.  c om*/
@Override
public List<UUID> getRequestingFacilities(List<UUID> supplyingFacilityIds) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<UUID> query = builder.createQuery(UUID.class);
    Root<Order> root = query.from(Order.class);

    if (!isEmpty(supplyingFacilityIds)) {
        Predicate predicate = builder.disjunction();
        for (Object elem : supplyingFacilityIds) {
            predicate = builder.or(predicate, builder.equal(root.get(SUPPLYING_FACILITY_ID), elem));
        }
        query.where(predicate);
    }

    query.select(root.get(REQUESTING_FACILITY_ID)).distinct(true);

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