List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
From source file:net.groupbuy.dao.impl.ConsultationDaoImpl.java
public List<Consultation> findList(Member member, Product product, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class); Root<Consultation> root = criteriaQuery.from(Consultation.class); criteriaQuery.select(root);//from www . j av a 2 s .c o m Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation"))); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (product != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product)); } if (isShow != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow)); } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:org.broadleafcommerce.cms.file.dao.StaticAssetDaoImpl.java
public List<StaticAsset> readAllStaticAssets() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<StaticAsset> criteria = builder.createQuery(StaticAsset.class); Root<StaticAssetImpl> handler = criteria.from(StaticAssetImpl.class); criteria.select(handler);/* w ww . ja va2 s . com*/ List<Predicate> restrictions = new ArrayList<Predicate>(); List<Order> sorts = new ArrayList<Order>(); 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()])); return em.createQuery(criteria).getResultList(); } catch (NoResultException e) { return new ArrayList<StaticAsset>(); } }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * Adds the filter criteria./* ww w . j av a2 s. com*/ * * @param userId * the user id * @param filters * the filters * @param builder * the builder * @param root * the root * @param query * the query */ private void addFilterCriteria(long userId, Map<String, Object> filters, CriteriaBuilder builder, Root<Transaction> root, CriteriaQuery query) { List<Predicate> predicates = new ArrayList<Predicate>(); if (!filters.isEmpty()) { for (Entry<String, Object> entry : filters.entrySet()) { if (CommonUtil.isNotNull(root.get(entry.getKey()))) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } } if (CommonUtil.isNotNull(userId)) { User user = userRepository.findById(userId); if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) { predicates.add(builder.equal(root.<Long>get(FIELDCONSTANT_USERID), user.getId())); } } query.where(predicates.toArray(new Predicate[] {})); }
From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java
@Override public List<Translation> readAllTranslationEntries(TranslatedEntity entityType, ResultType stage) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class); Root<TranslationImpl> root = criteria.from(TranslationImpl.class); criteria.select(root);/*w ww .jav a 2s.co m*/ List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType())); try { if (extensionManager != null) { extensionManager.getProxy().setup(TranslationImpl.class, stage); extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Translation> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); return query.getResultList(); } finally { if (extensionManager != null) { extensionManager.getProxy().breakdown(TranslationImpl.class, stage); } } }
From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java
@Override public Long countTranslationEntries(TranslatedEntity entityType, ResultType stage) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); Root<TranslationImpl> root = criteria.from(TranslationImpl.class); criteria.select(builder.count(root)); List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType())); try {/* ww w. j a va2 s.c om*/ if (extensionManager != null) { extensionManager.getProxy().setup(TranslationImpl.class, stage); extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Long> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); return query.getSingleResult(); } finally { if (extensionManager != null) { extensionManager.getProxy().breakdown(TranslationImpl.class, stage); } } }
From source file:com.uni.dao.etc.UniJpaRepository.java
/** * Applies the given {@link Specification} to the given {@link CriteriaQuery}. * /* ww w . ja v a2s . c om*/ * @param spec can be {@literal null}. * @param query must not be {@literal null}. * @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 = em.getCriteriaBuilder(); Predicate predicate = spec.toPredicate(root, query, builder); if (predicate != null) { query.where(predicate); } return root; }
From source file:eu.uqasar.service.user.UserService.java
public List<User> getAllExceptAndFilter(Collection<User> usersToExclude, final String filter) { if (usersToExclude == null || usersToExclude.isEmpty()) { return this.getAll(); }/*from w w w.j a v a 2 s.c o m*/ logger.infof("loading all Users except %s ...", usersToExclude); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> root = query.from(User.class); if (filter != null && !filter.isEmpty()) { Expression<String> literal = cb.upper(cb.literal(LIKE_WILDCARD + filter + LIKE_WILDCARD)); Predicate likeFirstName = cb.like(cb.upper(root.get(User_.firstName)), literal); Predicate likeLastName = cb.like(cb.upper(root.get(User_.lastName)), literal); Predicate likeUserName = cb.like(cb.upper(root.get(User_.userName)), literal); Predicate orLikeName = cb.or(likeFirstName, likeLastName, likeUserName); query.where(cb.and(cb.not(root.in(usersToExclude)), orLikeName)); } else { query.where(cb.not(root.in(usersToExclude))); } return em.createQuery(query).getResultList(); }
From source file:org.easy.criteria.CriteriaProcessor.java
/** * Counts the result found for the given criteria * /*from w ww . j av a2 s.c o m*/ * @param criteria * @param distinct * @return */ public <T> long count(final CriteriaComposer<T> criteria, boolean distinct) { log.trace("CriteriaProcessor.count"); Preconditions.checkNotNull(criteria); Class<T> forClass = criteria.getEntityClass(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); Root<T> root = criteriaQuery.from(forClass); log.debug("root =" + forClass.getName()); if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(root)); else criteriaQuery.select(criteriaBuilder.count(root)); List<Predicate> predicates = new ArrayList<Predicate>(); if (criteria != null) { criteria.generateJoins(root); criteria.generateWhere(criteriaBuilder, predicates); } criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])); TypedQuery<Long> query = entityManager.createQuery(criteriaQuery); long result = query.getSingleResult(); log.debug("CriteriaProcessor.count =" + result); return result; }
From source file:org.querybyexample.jpa.GenericRepository.java
/** * Count the number of E instances.//from w ww .j av a 2 s . c om * * @param entity a sample entity whose non-null properties may be used as * search hint * @param searchParameters carries additional search information * @return the number of entities matching the search. */ @Transactional(readOnly = true) public int findCount(E entity, SearchParameters sp) { checkNotNull(entity, "The entity cannot be null"); checkNotNull(sp, "The searchParameters cannot be null"); if (sp.hasNamedQuery()) { return byNamedQueryUtil.numberByNamedQuery(sp).intValue(); } CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class); Root<E> root = criteriaQuery.from(type); if (sp.getDistinct()) { criteriaQuery = criteriaQuery.select(builder.countDistinct(root)); } else { criteriaQuery = criteriaQuery.select(builder.count(root)); } // predicate Predicate predicate = getPredicate(root, builder, entity, sp); if (predicate != null) { criteriaQuery = criteriaQuery.where(predicate); } orderByUtil.forceJoinOrder(root, sp); TypedQuery<Long> typedQuery = entityManager.createQuery(criteriaQuery); applyCacheHints(typedQuery, sp); Long count = typedQuery.getSingleResult(); if (count != null) { return count.intValue(); } else { log.warn("findCount returned null"); return 0; } }
From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java
@Override public Map<MemberRegister, Integer> fetchTotalPointsPerMemberRegister(QuestionnaireType questionnaireType) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<EvaluationAnswer> evaluationAnswer = cq.from(EvaluationAnswer.class); Join<EvaluationAnswer, QuestionChoicesDef> choiceDef = evaluationAnswer.join(EvaluationAnswer_.answer); Join<QuestionChoicesDef, EvaluationQuestionDef> questionDef = choiceDef .join(QuestionChoicesDef_.evaluationQuestionDef); ParameterExpression<QuestionnaireType> questionnaireTypeParam = cb.parameter(QuestionnaireType.class, "questionnaireType"); Expression<Integer> value = evaluationAnswer.get(EvaluationAnswer_.answer).get(QuestionChoicesDef_.value) .as(Integer.class); cq.multiselect(evaluationAnswer.get(EvaluationAnswer_.memberRegister).alias("memberRegister"), value.alias("value")); cq.where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireTypeParam)); TypedQuery<Tuple> query = entityManager.createQuery(cq); query.setParameter("questionnaireType", questionnaireType); List<Tuple> result = query.getResultList(); Map<MemberRegister, Integer> pointsPerMemberRegister = result.stream().collect(Collectors.groupingBy(t -> { return t.get("memberRegister", MemberRegister.class); }, Collectors.summingInt(t -> t.get("value", Integer.class)))); return pointsPerMemberRegister; }