List of usage examples for javax.persistence.criteria Root join
<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);
From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java
@Override public ConcurrentMap<TermsOfConditionStatus, AtomicLong> fetchTermsOfCompetitionStatistic() { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<EvaluationAnswer> cq = cb.createQuery(EvaluationAnswer.class); 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> questionnaireType = cb.parameter(QuestionnaireType.class, "questionnaireType"); cq.select(evaluationAnswer);/*from w w w .j a v a 2 s. c o m*/ cq.where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireType)); TypedQuery<EvaluationAnswer> query = entityManager.createQuery(cq); query.setParameter("questionnaireType", QuestionnaireType.TERMS_OF_CONDITION); List<EvaluationAnswer> evaluationAnswers = query.getResultList(); ConcurrentMap<TermsOfConditionStatus, AtomicLong> distributionByTermsOfCompetitionStatus = new ConcurrentHashMap<>(); List<EvaluationQuestionnaireDefEnum> questionnaireDef = Arrays .asList(EvaluationQuestionnaireDefEnum.values()); long numberOfQuestion = questionnaireDef.stream() .filter(q -> q.getQuestionnaireType().equals(QuestionnaireType.TERMS_OF_CONDITION)) .collect(Collectors.counting()); List<MemberRegister> memberRegisters = evaluationAnswers.stream() .filter(StreamUtil.distinctByKey((EvaluationAnswer e) -> e.getMemberRegister().getId())) .map(EvaluationAnswer::getMemberRegister).collect(Collectors.toList()); memberRegisters.stream().forEach((memberRegister) -> { TermsOfConditionStatus termsOfConditionStatus = TermsOfConditionStatus.NONE; if (evaluationAnswers.stream().filter(m -> m.getMemberRegister().equals(memberRegister)) .count() == numberOfQuestion) { boolean isValid = evaluationAnswers.stream() .filter(m -> m.getMemberRegister().equals(memberRegister)) .allMatch(e -> e.getAnswer().getLabel() .equals(messageSource.getMessage("QuestionChoicesDef.yes", null, Locale.ENGLISH))); termsOfConditionStatus = isValid ? TermsOfConditionStatus.VALID : TermsOfConditionStatus.INVALID; } distributionByTermsOfCompetitionStatus.putIfAbsent(termsOfConditionStatus, new AtomicLong(0)); distributionByTermsOfCompetitionStatus.get(termsOfConditionStatus).incrementAndGet(); }); return distributionByTermsOfCompetitionStatus; }
From source file:com.expressui.core.dao.security.query.RelatedRolesQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Role> query, Root<Role> role) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(user)) { ParameterExpression<User> userExp = builder.parameter(User.class, "user"); predicates.add(builder.equal(role.join("userRoles").get("user"), userExp)); }/* ww w. j a v a 2s . co m*/ return predicates; }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Integer getSalesVolume(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w ww . j av a 2 s . c om*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceGroupRepository.java
/** * * @param name//from ww w . j a v a2 s.c o m * @param resourceTypeId * @return */ public ResourceGroupEntity loadUniqueGroupByNameAndType(String name, Integer resourceTypeId) { ResourceGroupEntity result = null; try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ResourceGroupEntity> q = cb.createQuery(ResourceGroupEntity.class); Root<ResourceGroupEntity> r = q.from(ResourceGroupEntity.class); r.fetch("resources"); Join<ResourceGroupEntity, ResourceEntity> resources = r.join("resources"); Predicate typePred = cb.equal(resources.get("resourceType").get("id"), resourceTypeId); Predicate resNamePred = cb.equal(resources.get("name"), name); q.where(cb.and(typePred, resNamePred)); q.distinct(true); result = entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { // do nothing } return result; }
From source file:com.movies.dao.impl.BaseDaoImpl.java
@Override public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass, List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes, List<SetAttribute> setAttributes) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true); Root<T> root = cq.from(returnClass); if (CollectionUtils.isNotEmpty(singleAttributes)) { for (SingularAttribute attribute : singleAttributes) { root.join(attribute); root.fetch(attribute);/*ww w .j a v a 2 s .c o m*/ } } if (CollectionUtils.isNotEmpty(listAttributes)) { for (ListAttribute attribute : listAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } if (CollectionUtils.isNotEmpty(setAttributes)) { for (SetAttribute attribute : setAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } Set<Entry<String, Object>> set = map.entrySet(); int numberOfClauses = set.size(); Predicate[] predicates = new Predicate[numberOfClauses]; int i = 0; for (Entry<String, Object> entry : set) { String key = entry.getKey(); if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) { predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE); } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) { //predicates[i++] = cb.equal( ,entry.getValue()); } else { predicates[i++] = cb.equal(root.get(key), entry.getValue()); } } return em.createQuery(cq.select(root).where(predicates)).getResultList(); }
From source file:com.expressui.core.dao.security.query.RelatedUsersQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(role)) { ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role"); predicates.add(builder.equal(user.join("userRoles").get("role"), roleExp)); }//ww w .j a va 2 s . c o m if (hasValue(doesNotBelongToRole)) { Subquery<User> subquery = query.subquery(User.class); Root userRole = subquery.from(UserRole.class); ParameterExpression<Role> roleExp = builder.parameter(Role.class, "doesNotBelongToRole"); subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), roleExp)); predicates.add(builder.not(user.in(subquery))); } return predicates; }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override//from w w w . j a va 2s. c o m public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.select(productJoin); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override//from w ww . j av a 2s . c o m public List<ImageProductDTO> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ImageProductDTO> query = cb.createQuery(ImageProductDTO.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); query.select(cb.construct(ImageProductDTO.class, imageRoot.get(Image_.name), productJoin.get(Product_.name))).orderBy(cb.asc(imageRoot.get(Image_.name))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercilessly() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override//from w w w . j a va 2 s . co m public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> product = query.from(Product.class); query.select(product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%")); Subquery<Long> subQuery = query.subquery(Long.class); Root<Image> infoRoot = subQuery.from(Image.class); Join<Image, Product> productJoin = infoRoot.join(Image_.product); subQuery.select(productJoin.<Long>get(Product_.id)); subQuery.where(cb.gt(infoRoot.get(Image_.index), 0)); criteria.add(cb.in(product.get(Product_.id)).value(subQuery)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java
private Query applicationServersForSuggestBox(String input) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<String> q = cb.createQuery(String.class); Root<ResourceEntity> r = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType"); Predicate appServerNamePred = cb.like(resType.<String>get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()); input = input + "%"; q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input))); q.select(r.<String>get("name")); q.distinct(true);/*from w ww . j av a 2 s . com*/ q.orderBy(cb.asc(r.get("name"))); return entityManager.createQuery(q); }