List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
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; }
From source file:de.ks.idnadrev.expimp.xls.XlsxExporterTest.java
protected List<Long> getAllIds() { return PersistentWork.read(em -> { CriteriaQuery<Long> criteriaQuery = em.getCriteriaBuilder().createQuery(Long.class); Root<Thought> root = criteriaQuery.from(Thought.class); Path<Long> id = root.<Long>get("id"); criteriaQuery.select(id);/*from w ww.j a v a 2 s . c o m*/ return em.createQuery(criteriaQuery).getResultList(); }); }
From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java
@Override @Transactional(readOnly = true)//from w ww. j a va 2s. co m public ExampleGeneratedIdEntity findWithOthersFetchJoin(final Long id) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder .createQuery(ExampleGeneratedIdEntity.class); final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass); root.fetch(ExampleGeneratedIdEntity_.otherEntities); criteriaQuery.where(builder.equal(root.get(ExampleGeneratedIdEntity_.id), id)); return this.findSingle(criteriaQuery); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication, Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);/*from www . j a v a 2 s .c om*/ Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } if (generateMethod != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("generateMethod"), generateMethod)); } 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.findList(criteriaQuery, first, count, null, null); }
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);//from w w w . ja v a 2 s.c o m final TypedQuery<Device> query = createQuery(criteria); return query.getResultList().size(); }
From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java
/** * Returns a unsorted list of distinct values of the requested field. * * @param field Model field name./*from ww w. ja v a2 s . c om*/ * @return Sorted list of distinct values of {@code field}. */ public Iterable<Object> distinct(String field) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType()); Root<T> root = query.from(metadata.getJavaType()); query.select(root.get(field)).distinct(true); return (List) entityManager.createQuery(query).getResultList(); }
From source file:gov.gtas.repository.CaseDispositionRepositoryImpl.java
@Override public Pair<Long, List<Case>> findByCriteria(CaseRequestDto dto) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Case> q = cb.createQuery(Case.class); Root<Case> root = q.from(Case.class); List<Predicate> predicates = new ArrayList<>(); TypedQuery<Case> typedQuery = em.createQuery(q); // sorting// w w w . j a v a2s. c om if (dto.getSort() != null) { List<Order> orders = new ArrayList<>(); for (SortOptionsDto sort : dto.getSort()) { Expression<?> e = root.get(sort.getColumn()); Order order; if ("desc".equalsIgnoreCase(sort.getDir())) { order = cb.desc(e); } else { order = cb.asc(e); } orders.add(order); } q.orderBy(orders); } if (dto.getFlightId() != null) { predicates.add(cb.equal(root.<Long>get("flightId"), dto.getFlightId())); } if (dto.getPaxId() != null) { predicates.add(cb.equal(root.<Long>get("paxId"), dto.getPaxId())); } if (dto.getPaxName() != null) { String likeString = String.format("%%%s%%", dto.getPaxName().toUpperCase()); predicates.add(cb.like(root.<String>get("paxName"), likeString)); } if (dto.getLastName() != null) { // map this to full pax name String likeString = String.format("%%%s%%", dto.getLastName().toUpperCase()); predicates.add(cb.like(root.<String>get("paxName"), likeString)); } if (dto.getStatus() != null) { String likeString = String.format("%%%s%%", dto.getStatus().toUpperCase()); predicates.add(cb.like(root.<String>get("status"), likeString)); } if (dto.getFlightNumber() != null) { predicates.add(cb.equal(root.<Long>get("flightNumber"), dto.getFlightNumber())); } if (dto.getRuleCatId() != null) { predicates.add(cb.equal(root.<Long>get("highPriorityRuleCatId"), dto.getRuleCatId())); } Predicate etaCondition; if (dto.getEtaStart() != null && dto.getEtaEnd() != null) { Path<Date> eta = root.<Date>get("flightETADate"); Predicate startPredicate = cb.or(cb.isNull(eta), cb.greaterThanOrEqualTo(eta, dto.getEtaStart())); Predicate endPredicate = cb.or(cb.isNull(eta), cb.lessThanOrEqualTo(eta, dto.getEtaEnd())); etaCondition = cb.and(startPredicate, endPredicate); predicates.add(etaCondition); } q.select(root).where(predicates.toArray(new Predicate[] {})); typedQuery = em.createQuery(q); // total count CriteriaQuery<Long> countQuery = cb.createQuery(Long.class); countQuery.select(cb.count(countQuery.from(Case.class))).where(predicates.toArray(new Predicate[] {})); Long count = em.createQuery(countQuery).getSingleResult(); // pagination int pageNumber = dto.getPageNumber(); int pageSize = dto.getPageSize(); int firstResultIndex = (pageNumber - 1) * pageSize; typedQuery.setFirstResult(firstResultIndex); typedQuery.setMaxResults(dto.getPageSize()); logger.debug(typedQuery.unwrap(org.hibernate.Query.class).getQueryString()); List<Case> results = typedQuery.getResultList(); return new ImmutablePair<>(count, results); }
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);// www .jav a2 s .c o m final TypedQuery<Device> query = createQuery(criteria); CacheHelper.cacheable(query); return query.getResultList().stream().map(Device::convertToVo).collect(Collectors.toList()); }
From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java
/** * Returns a unsorted list of distinct values of the requested field, filtered using a {@link QueryCriteria} * based query./*from w ww . j a v a2 s. c o m*/ * * @param field Model field name. * @param queryCriterias Query criteria to filter the field values by. * @return Sorted list of distinct values of {@code field}. */ public Iterable<Object> distinct(String field, Iterable<QueryCriteria> queryCriterias) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType()); Root<T> root = query.from(metadata.getJavaType()); query.where(queryBuilder.createSpecification(queryCriterias).toPredicate(root, query, builder)); query.select(root.get(field)).distinct(true); return (List) entityManager.createQuery(query).getResultList(); }
From source file:org.sloth.persistence.impl.ReportDaoImpl.java
@Override public Collection<Report> getAll() { CriteriaQuery<Report> cq = getEntityManager().getCriteriaBuilder().createQuery(Report.class); cq.select(cq.from(Report.class)); Collection<Report> list = getEntityManager().createQuery(cq).getResultList(); logger.info("Getting all Reports; Found: {}", list.size()); return list;/*from ww w . jav a2 s . c o m*/ }