List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.java
/** * Adds the filter criteria./*from w ww . j a v a 2 s . c om*/ * * @param filters the filters * @param builder the builder * @param root the root * @param query the query */ private void addFilterCriteria(Map<String, Object> filters, CriteriaBuilder builder, Root<CreditCard> root, CriteriaQuery query) { if (!filters.isEmpty()) { List<Predicate> predicates = new ArrayList<Predicate>(); for (Entry<String, Object> entry : filters.entrySet()) { if (entry.getKey().equalsIgnoreCase(FIELDCONSTANT_CARDNUMBER) || entry.getKey().equalsIgnoreCase(FIELDCONSTANT_CREDITLIMIT) || entry.getKey().equalsIgnoreCase(FIELDCONSTANT_BALANCE)) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } query.where(predicates.toArray(new Predicate[] {})); } }
From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java
public Long findFilteredCount(String search, List<String> searchProperties) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<ENTITY> from = query.from(entityClass); query.select(builder.count(from));//from w w w .j av a 2 s. com if (!Strings.isNullOrEmpty(search)) { Predicate[] buildFilterPredicates = BuildFilterPredicates(from, search, searchProperties); if (buildFilterPredicates.length > 0) { query.where(builder.or(buildFilterPredicates)); } } return count(entityManager.createQuery(query)); }
From source file:org.synyx.hades.dao.orm.GenericJpaDao.java
/** * Applies the given {@link Specification} to the given * {@link CriteriaQuery}./* w w w. ja v a2 s . c om*/ * * @param spec can be {@literal null} * @param query * @return */ private <S> Root<T> applySpecificationToCriteria(Specification<T> spec, CriteriaQuery<S> query) { Assert.notNull(query); Root<T> root = query.from(getDomainClass()); if (spec == null) { return root; } CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); Predicate predicate = spec.toPredicate(root, query, builder); if (predicate != null) { query.where(predicate); } return root; }
From source file:com.sapito.db.dao.AbstractDao.java
public List<T> findBySpecificField(String field, Object fieldContent, String predicates, LinkedHashMap<String, String> ordering, LinkedList<String> grouping) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> root = cq.from(entityClass); Predicate predicate = null;/*from ww w . j a v a 2s . c o m*/ if (predicates.equals("equal")) { predicate = cb.equal(root.get(field), fieldContent); } else if (predicates.equals("likelower")) { predicate = cb.like(cb.lower(root.<String>get(field)), fieldContent.toString()); } else if (predicates.equals("like")) { predicate = cb.like(root.<String>get(field), "%" + fieldContent.toString() + "%"); } cq.select(root); cq.where(predicate); if (ordering != null) { Set<String> set = ordering.keySet(); List<Order> orders = new ArrayList<>(); for (String orderingField : set) { Order order; if (ordering.get(orderingField).equals("ASC")) { order = cb.asc(root.get(orderingField)); } else { order = cb.desc(root.get(orderingField)); } orders.add(order); } cq.orderBy(orders); } if (grouping != null) { Iterator iterator = grouping.iterator(); List<Expression> groups = new LinkedList<>(); while (iterator.hasNext()) { groups.add(root.get(iterator.next().toString())); } cq.groupBy(groups); } Query query = entityManager.createQuery(cq); query.setMaxResults(MAX_RECORDS_RETURNED); return query.getResultList(); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find an entity of the given type, using as filter criteria the specified * list of attribute values. The filter criteria must uniquely identify an * entity./* w w w. ja v a 2 s.c o m*/ * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> T findEntityByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (T) query.getSingleResult(); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find a list of entities of the given type, using as filter criteria the * specified list of attribute values.//from w ww.j av a 2 s . c o m * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (List<T>) query.getResultList(); }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * Adds the filter criteria.//from w w w. j a v a 2 s . c o m * * @param filters the filters * @param builder the builder * @param root the root * @param query the query */ private void addFilterCriteria(Map<String, Object> filters, CriteriaBuilder builder, Root<User> root, CriteriaQuery query) { if (!filters.isEmpty()) { List<Predicate> predicates = new ArrayList<Predicate>(); for (Entry<String, Object> entry : filters.entrySet()) { if (!entry.getKey().equalsIgnoreCase(FIELDCONSTANT_BALANCE) && !entry.getKey().equalsIgnoreCase(FIELDCONSTANT_CARDNUMBER) && !entry.getKey().equalsIgnoreCase(FIELDCONSTANT_CREDITLIMIT)) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } query.where(predicates.toArray(new Predicate[] {})); } }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /*from w w w . j a va 2s.c o m*/ * @param first * @param count * @param adapter * @return */ public List<JiraMetricMeasurement> getAllByAdapter(int first, int count, AdapterSettings adapter) { logger.info("Get measurements for adapter: " + adapter); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.adapter), adapter); query.orderBy(cb.asc(root.get(JiraMetricMeasurement_.jiraKey))); query.where(condition); return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java
@Override public StaticAsset readStaticAssetById(Long id) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler);//from w ww. jav a2s. c o m List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(handler.get("id"), id)); if (queryExtensionManager != null) { queryExtensionManager.getProxy().setup(StaticAssetImpl.class, null); queryExtensionManager.getProxy().refineRetrieve(StaticAssetImpl.class, null, builder, criteria, handler, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); 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; }
From source file:net.shopxx.dao.impl.MemberDaoImpl.java
public Long registerMemberCount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class); Root<Member> root = criteriaQuery.from(Member.class); criteriaQuery.select(root);/*from w w w. ja v a 2s. 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); }