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.devicehive.dao.rdbms.DeviceDaoRdbmsImpl.java

@Override
public List<DeviceVO> list(String name, String namePattern, Long networkId, String networkName,
        Long deviceClassId, String deviceClassName, String sortField, @NotNull Boolean sortOrderAsc,
        Integer take, Integer skip, 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, ofNullable(name),
            ofNullable(namePattern), ofNullable(networkId), ofNullable(networkName), ofNullable(deviceClassId),
            ofNullable(deviceClassName), ofNullable(principal));

    criteria.where(predicates);
    CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), sortOrderAsc);

    final TypedQuery<Device> query = createQuery(criteria);
    cacheQuery(query, of(CacheConfig.refresh()));
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);
    List<Device> resultList = query.getResultList();
    return resultList.stream().map(Device::convertToVo).collect(Collectors.toList());
}

From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java

/**
 * {@inheritDoc}//from   w w  w.j  a  va 2  s . co  m
 */
public List<Transaction> findTransactionsByStatementDateAndCreditCardId(Date startDate, Date endDate,
        long creditCardId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    Predicate date = builder.greaterThan(root.<Date>get(FIELD_CONSTANT_TRANSACTION_DATE), startDate);
    Predicate isCreditCardId = builder.equal(root.<String>get(FIELD_CONSTANT_CREDIT_CARD_ID), creditCardId);
    query.where(builder.and(date, isCreditCardId));
    return super.find(query);
}

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

public boolean ldapBasedUserExists(final LdapUser user) {
    logger.infof("looking for users with username %s or mail %s", user.getUserName(), user.getMail());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate userName = cb.equal(root.get(User_.userName), user.getUserName());
    Predicate mail = cb.equal(root.get(User_.mail), user.getMail());
    query.where(cb.or(userName, mail));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    return !resultList.isEmpty();
}

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

public User getLdapBasedUser(final LdapUser user) {
    logger.infof("looking for LDAP users with username %s or mail %s", user.getUserName(), user.getMail());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate userName = cb.equal(root.get(User_.userName), user.getUserName());
    Predicate mail = cb.equal(root.get(User_.mail), user.getMail());
    query.where(cb.and(cb.or(userName, mail)));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    return resultList.isEmpty() ? null : resultList.get(0);
}

From source file:org.broadleafcommerce.core.order.dao.OrderDaoImpl.java

@Override
public List<Order> readOrdersByIds(List<Long> orderIds) {
    if (orderIds == null || orderIds.size() == 0) {
        return null;
    }//from  www  .  ja v  a2s. com
    if (orderIds.size() > 100) {
        LOG.warn("Not recommended to use the readOrdersByIds method for long lists of orderIds, since "
                + "Hibernate is required to transform the distinct results. The list of requested"
                + "order ids was (" + orderIds.size() + ") in length.");
    }
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
    Root<OrderImpl> order = criteria.from(OrderImpl.class);
    criteria.select(order);

    // We only want results that match the order IDs
    criteria.where(order.get("id").as(Long.class).in(orderIds));

    TypedQuery<Order> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");

    return query.getResultList();
}

From source file:org.broadleafcommerce.openadmin.server.security.dao.AdminPermissionDaoImpl.java

@Override
public AdminPermission readAdminPermissionByName(String name) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
    Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
    criteria.select(adminPerm);/*  w w  w .j  a v  a  2 s.c o  m*/

    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(adminPerm.get("name"), name));

    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<AdminPermission> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<AdminPermission> results = query.getResultList();
    if (results == null || results.size() == 0) {
        return null;
    } else {
        return results.get(0);
    }
}

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();

}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletEntityDao.java

@Override
public void afterPropertiesSet() throws Exception {
    this.userIdParameter = this.createParameterExpression(Integer.class, "userId");
    this.portletDefinitionParameter = this.createParameterExpression(PortletDefinitionImpl.class,
            "portletDefinition");

    this.findEntitiesForDefinitionQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletEntityImpl>>() {
                @Override//from   w ww .ja  v a2s.c  o  m
                public CriteriaQuery<PortletEntityImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletEntityImpl> criteriaQuery = cb
                            .createQuery(PortletEntityImpl.class);
                    final Root<PortletEntityImpl> entityRoot = criteriaQuery.from(PortletEntityImpl.class);
                    criteriaQuery.select(entityRoot);
                    addFetches(entityRoot);
                    criteriaQuery.where(cb.equal(entityRoot.get(PortletEntityImpl_.portletDefinition),
                            portletDefinitionParameter));

                    return criteriaQuery;
                }
            });

    this.findEntitiesForUserIdQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletEntityImpl>>() {
                @Override
                public CriteriaQuery<PortletEntityImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletEntityImpl> criteriaQuery = cb
                            .createQuery(PortletEntityImpl.class);
                    final Root<PortletEntityImpl> entityRoot = criteriaQuery.from(PortletEntityImpl.class);
                    criteriaQuery.select(entityRoot);
                    addFetches(entityRoot);
                    criteriaQuery.where(cb.equal(entityRoot.get(PortletEntityImpl_.userId), userIdParameter));

                    return criteriaQuery;
                }
            });
}

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

public <T extends ResourceEntity, V> boolean isUniqueAttributeAlreadyTaken(String attributeValue, String id,
        SingularAttribute<? super T, V> attribute, Class<T> clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<T> resource = cq.from(clazz);

    cq.select(cb.countDistinct(resource));

    Predicate predicate = cb.equal(resource.get(attribute), attributeValue);
    if (id != null) {
        Predicate ignoreId = cb.notEqual(resource.get(ResourceEntity_.id), id);
        predicate = cb.and(predicate, ignoreId);
    }//ww w  .ja  va2s  .co  m
    cq.where(predicate);

    TypedQuery<Long> countQuery = em.createQuery(cq);

    return countQuery.getSingleResult() > 0;
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.ConferenceUserDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.emailParameter = this.createParameterExpression(String.class, "email");

    this.getUsersByPrimaryEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override/*from w ww .  j  a  v  a2 s.c o m*/
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery
                            .where(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter));

                    return criteriaQuery;
                }
            });

    this.getUsersByAnyEmailQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<ConferenceUserImpl>>() {
                @Override
                public CriteriaQuery<ConferenceUserImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<ConferenceUserImpl> criteriaQuery = cb
                            .createQuery(ConferenceUserImpl.class);
                    final Root<ConferenceUserImpl> definitionRoot = criteriaQuery
                            .from(ConferenceUserImpl.class);
                    criteriaQuery.select(definitionRoot);
                    criteriaQuery.where(
                            cb.or(cb.equal(definitionRoot.get(ConferenceUserImpl_.email), emailParameter),
                                    cb.isMember(emailParameter,
                                            definitionRoot.get(ConferenceUserImpl_.additionalEmails))));

                    return criteriaQuery;
                }
            });
}