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:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java

/**
 * Test retrieve limited user./*  www  .ja  va  2s  .c o m*/
 */
@Test
public void testRetrieveLimitedUser() {

    int limit = 10;
    int start = 5;

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

    List<User> user = entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit)
            .getResultList();

    assertEquals(user.size(), 5);
}

From source file:com.devicehive.dao.rdbms.DeviceClassDaoRdbmsImpl.java

@Override
public List<DeviceClassWithEquipmentVO> list(String name, String namePattern, String sortField,
        Boolean sortOrderAsc, Integer take, Integer skip) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<DeviceClass> criteria = cb.createQuery(DeviceClass.class);
    final Root<DeviceClass> from = criteria.from(DeviceClass.class);

    final Predicate[] predicates = CriteriaHelper.deviceClassListPredicates(cb, from, ofNullable(name),
            ofNullable(namePattern));//w  ww.j av a  2 s .c  o  m
    criteria.where(predicates);
    CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), Boolean.TRUE.equals(sortOrderAsc));

    final TypedQuery<DeviceClass> query = createQuery(criteria);
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);

    CacheHelper.cacheable(query);
    List<DeviceClass> resultList = query.getResultList();
    Stream<DeviceClassWithEquipmentVO> objectStream = resultList.stream().map(DeviceClass::convertToVo);
    return objectStream.collect(Collectors.toList());
}

From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoArvoDAOImpl.java

/**
 * Returns yhteystietoarvos for a given yhteystietojen tyyppi
 * @param yhteystietojenTyyppi the yhteystietojen tyyppi given
 * @return the yhteystietoarvo objects matching the given yhteystietojen tyyppi
 *///from www .j a va 2  s .  co  m
@Override
public List<YhteystietoArvo> findByYhteystietojenTyyppi(YhteystietojenTyyppi yhteystietojenTyyppi) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<YhteystietoArvo> query = cb.createQuery(YhteystietoArvo.class);

    Root<YhteystietoArvo> root = query.from(YhteystietoArvo.class);
    query.select(root);

    Predicate yhteystietojenTyyppiEquals = cb.equal(root.get("kentta").get("yhteystietojenTyyppi").get("oid"),
            yhteystietojenTyyppi.getOid());
    query.where(yhteystietojenTyyppiEquals);

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

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

public List<Process> getAllByAscendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.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  a v a 2  s .c o  m*/

    criteria.orderBy(cb.asc(root.get(Process_.endDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public List<Process> getAllByDescendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from   w w  w  .j  a v a2  s.  c  o m*/

    criteria.orderBy(cb.desc(root.get(Process_.endDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public List<Process> getAllByDescendingStartDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.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 av  a  2s.  c  o  m*/

    criteria.orderBy(cb.desc(root.get(Process_.startDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public List<Process> getAllAscendingStartDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//w ww  .j  ava  2 s.co m

    criteria.orderBy(cb.asc(root.get(Process_.startDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao,
        RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    removeHtmlEncoding(entity);//w ww . j  av a2s.c o m
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(
                StructuredContentItemCriteria.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao
                .retrieve(Class.forName(entity.getType()[0]), primaryKey);
        if (adminInstance.getStructuredContent().getLockedFlag()) {
            /*
            This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance
             */
            CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
            CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class);
            Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class);
            query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE),
                    criteriaBuilder.equal(root.get("originalItemId"),
                            adminInstance.getStructuredContent().getId())));
            query.select(root);
            TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager()
                    .createQuery(query);
            try {
                checkCriteria: {
                    StructuredContent myContent = scQuery.getSingleResult();
                    for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) {
                        if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule())
                                && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) {
                            //manually set the values - otherwise unwanted properties will be set
                            itemCriteria.setOrderItemMatchRule(
                                    entity.findProperty("orderItemMatchRule").getValue());
                            itemCriteria
                                    .setQuantity(Integer.parseInt(entity.findProperty("quantity").getValue()));
                            adminInstance = itemCriteria;
                            break checkCriteria;
                        }
                    }
                    throw new RuntimeException("Unable to find an item criteria to update");
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to update a locked record");
            }
        } else {
            adminInstance = (StructuredContentItemCriteria) helper.createPopulatedInstance(adminInstance,
                    entity, adminProperties, false);
        }
        adminInstance = (StructuredContentItemCriteria) dynamicEntityDao.merge(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);

        return adminEntity;
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}

From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

@Override
public long getAllowedDeviceCount(HivePrincipal principal, List<String> guids) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<Device> criteria = cb.createQuery(Device.class);
    final Root<Device> from = criteria.from(Device.class);
    final Predicate[] predicates = CriteriaHelper.deviceListPredicates(cb, from, guids,
            Optional.ofNullable(principal));
    criteria.where(predicates);
    final TypedQuery<Device> query = createQuery(criteria);
    return query.getResultList().size();
}

From source file:com.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

@Override
public List<DeviceVO> getDeviceList(List<String> guids, HivePrincipal principal) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<Device> criteria = cb.createQuery(Device.class);
    final Root<Device> from = criteria.from(Device.class);
    final Predicate[] predicates = CriteriaHelper.deviceListPredicates(cb, from, guids,
            Optional.ofNullable(principal));
    criteria.where(predicates);
    final TypedQuery<Device> query = createQuery(criteria);
    CacheHelper.cacheable(query);//from  w w w  .  j a  v a 2 s .  c om
    return query.getResultList().stream().map(Device::convertToVo).collect(Collectors.toList());
}