List of usage examples for javax.persistence.criteria Root join
<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);
From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java
@Override @Transactional(readOnly = true)/*from ww w . j av a2 s . com*/ public List<ExampleGeneratedIdEntity> findByOtherText(final String text) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder.createQuery(this.entityClass); criteriaQuery.distinct(true); final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass); final ListJoin<ExampleGeneratedIdEntity, ExampleIdEntity> join = root .join(ExampleGeneratedIdEntity_.otherEntities); criteriaQuery.where(builder.equal(join.get(ExampleIdEntity_.text), text)); return this.find(criteriaQuery); }
From source file:org.verinice.persistence.VeriniceAccountDaoImpl.java
private TypedQuery<Entity> buildQueryForProperties(String loginName) { CriteriaBuilder builder = getCriteriaBuilder(); CriteriaQuery<Entity> query = builder.createQuery(Entity.class); Root<Entity> entity = query.from(Entity.class); Join<PropertyList, Entity> propertyListJoin = entity.join("propertyLists"); Join<PropertyList, Property> propertiesJoin = propertyListJoin.join("properties"); List<Predicate> conditions = new ArrayList<>(); Path<String> propertytypePath = propertiesJoin.get("propertytype"); conditions.add(builder.like(propertytypePath, "configuration_benutzername")); Path<String> propertyvaluePath = propertiesJoin.get("propertyvalue"); conditions.add(builder.like(propertyvaluePath, loginName)); query.where(conditions.toArray(new Predicate[conditions.size()])); return entityManager.createQuery(query); }
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public Page<Article> findPage(ArticleCategory articleCategory, List<Tag> tags, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from w ww. j a v a2 s. co m Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (tags != null && !tags.isEmpty()) { Subquery<Article> subquery = criteriaQuery.subquery(Article.class); Root<Article> subqueryRoot = subquery.from(Article.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findPage(criteriaQuery, pageable); }
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, List<Tag> tags, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from ww w. ja v a2 s . co m Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (tags != null && !tags.isEmpty()) { Subquery<Article> subquery = criteriaQuery.subquery(Article.class); Root<Article> subqueryRoot = subquery.from(Article.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:org.sloth.persistence.impl.ObservationDaoImpl.java
@Override public Collection<Observation> getByKeyWord(String key) throws NullPointerException { if (key == null) { throw new NullPointerException(); }/*ww w .ja v a2s. c om*/ key = "%" + key.trim().replace('*', '%').toUpperCase() + "%"; CriteriaBuilder b = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Observation> q = b.createQuery(Observation.class); Root<Observation> o = q.from(Observation.class); Join<Observation, Categorie> j = o.join(Observation_.categorie); Collection<Observation> result = getEntityManager().createQuery(q.select(o).distinct(true) .where(b.or(b.like(b.upper(j.get(Categorie_.title)), key), b.like(b.upper(j.get(Categorie_.description)), key), b.like(b.upper(o.get(Observation_.title)), key), b.like(b.upper(o.get(Observation_.description)), key)))) .getResultList(); logger.info("Searched for {}. Got {} Results.", key, result.size()); return result; }
From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java
@Override public Long fetchNumberOfMemberRegistersWithoutTerms() { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<MemberRegister> memberRegister = cq.from(MemberRegister.class); Subquery<Long> sq = cq.subquery(Long.class); Root<EvaluationAnswer> evaluationAnswer = sq.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"); sq.select(evaluationAnswer.get("memberRegister").get("id")) .where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireType)); cq.select(cb.count(memberRegister.get("id"))).where(cb.not(cb.in(memberRegister.get("id")).value(sq))); TypedQuery<Long> query = entityManager.createQuery(cq); query.setParameter("questionnaireType", QuestionnaireType.TERMS_OF_CONDITION); return query.getSingleResult(); }
From source file:core.commonapp.server.dao.party.PersonDaoHibernateImpl.java
@Override @Transactional/*w w w . j a v a 2 s .c om*/ public List<Person> findAllContactPeople(Integer partyId) { CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Person> query = builder.createQuery(Person.class); Root<Person> contactPerson = query.from(Person.class); Join<Person, PartyRelationship> partyToRelationships = contactPerson.join("partyToRelationships"); query.where(builder.equal(partyToRelationships.get("partyFrom.partyId"), partyId), builder.isNull(partyToRelationships.get("thruDate"))); List<Person> people = getEntityManager().createQuery(query).getResultList(); PartyDaoHibernateImpl.lazyLoad((Party) people, true, true, false, false); return people; }
From source file:org.pushio.webapp.support.persistence.DynamicSpecifications.java
/** * @see ??JPA/* w ww . j a v a2 s .c o m*/ * @param filters * @param entityClazz * @param isDistinct trueSQLdistinctfalse? * @return */ public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz, final boolean isDistinct, final List<OrderParam> orderParams) { return new Specification<T>() { @Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (orderParams != null && orderParams.size() > 0) { /* CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); Root<Foo> from = criteriaQuery.from(Foo.class); CriteriaQuery<Foo> select = criteriaQuery.select(from); criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); */ List<Order> orders = new ArrayList<Order>(orderParams.size()); for (OrderParam orderParam : orderParams) { if (orderParam != null && orderParam.getField() != null) { String fields[] = StringUtil.split(orderParam.getField(), '.'); Path expression = (fields.length > 1) ? root.join(fields[0]) : root.get(fields[0]); for (int i = 1, len = fields.length; i < len; ++i) { expression = expression.get(fields[i]); } if (expression != null) { Order order = (orderParam.getType() == null || orderParam.getType().equalsIgnoreCase("asc")) ? builder.asc(expression) : builder.desc(expression); orders.add(order); // query.orderBy(order); } } } query.orderBy(orders); } query.distinct(isDistinct); if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, '.'); Path expression = null; ////// ? boolean hasJoin = names[0].startsWith("[join]"); String fn = hasJoin ? names[0].substring(6) : names[0]; boolean isNotDateType = !(filter.value instanceof Date); try { expression = hasJoin ? root.join(fn) : root.get(fn); if (isNotDateType && isDateType(expression.getJavaType())) { // filter.value?? filter.value = parseDate(filter.value.toString(), filter.operator); } } catch (Exception e) { // logger.error(e.getMessage(), e); continue; // ?? } boolean isPropertyNotValid = false; for (int i = 1; i < names.length; i++) { try { expression = expression.get(names[i]); if (isNotDateType && isDateType(expression.getJavaType())) { filter.value = parseDate(filter.value.toString(), filter.operator); } } catch (Exception e) { // logger.error(e.getMessage(), e); isPropertyNotValid = true; // break; // ?? } } if (expression == null || isPropertyNotValid) { continue; } /////// // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.like(expression, "%" + values[i] + "%"); } predicates.add(builder.or(like)); } break; case ANDLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.like(expression, "%" + values[i] + "%"); } predicates.add(builder.and(like)); } break; case ANDNOTLIKE: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.notLike(expression, "%" + values[i] + "%"); } predicates.add(builder.and(like)); } break; case OREQ: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.equal(expression, values[i]); } predicates.add(builder.or(like)); } break; case ANDNOTEQ: if (filter.value instanceof String) { String value = (String) filter.value; String[] values = value.split(","); Predicate[] like = new Predicate[values.length]; for (int i = 0; i < values.length; i++) { like[i] = builder.notEqual(expression, values[i]); } predicates.add(builder.and(like)); } break; case NOTEQ: predicates.add(builder.notEqual(expression, (Comparable) filter.value)); break; case NOTLIKE: predicates.add(builder.notLike(expression, "%" + filter.value + "%")); break; case NULL: predicates.add(builder.isNull(expression)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; // case IN: // predicates.add(builder.in(expression).in(values)); // break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
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:se.kth.csc.persist.JPAStore.java
@Override public QueuePosition fetchQueuePositionWithQueueAndUser(String queueName, String userName) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<QueuePosition> q = cb.createQuery(QueuePosition.class); Root<QueuePosition> queuePosition = q.from(QueuePosition.class); Join<QueuePosition, Queue> queue = queuePosition.join(QueuePosition_.queue); Join<QueuePosition, Account> account = queuePosition.join(QueuePosition_.account); try {//w w w . j a v a 2 s . c o m return entityManager .createQuery( q.select(queuePosition) .where(cb.and(cb.equal(queue.get(Queue_.name), queueName), cb.equal(account.get(Account_.principalName), userName)))) .getSingleResult(); } catch (NoResultException e) { return null; } }