Example usage for javax.persistence.criteria Subquery select

List of usage examples for javax.persistence.criteria Subquery select

Introduction

In this page you can find the example usage for javax.persistence.criteria Subquery select.

Prototype

Subquery<T> select(Expression<T> expression);

Source Link

Document

Specify the item that is to be returned as the subquery result.

Usage

From source file:org.wallride.repository.ArticleSpecifications.java

public static Specification<Article> draft(Article article) {
    return (root, query, cb) -> {
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(root.get(Article_.drafted), article));

        Subquery<Long> subquery = query.subquery(Long.class);
        Root<Article> p = subquery.from(Article.class);
        subquery.select(cb.max(p.get(Article_.id))).where(cb.equal(p.get(Article_.drafted), article));

        predicates.add(cb.equal(root.get(Article_.id), subquery));
        return cb.and(predicates.toArray(new Predicate[0]));
    };/*from w  w  w.  j av a  2  s.  co  m*/
}

From source file:org.wallride.repository.PageSpecifications.java

public static Specification<Page> draft(Page page) {
    return (root, query, cb) -> {
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(root.get(Page_.drafted), page));

        Subquery<Long> subquery = query.subquery(Long.class);
        Root<Page> p = subquery.from(Page.class);
        subquery.select(cb.max(p.get(Page_.id))).where(cb.equal(p.get(Page_.drafted), page));

        predicates.add(cb.equal(root.get(Page_.id), subquery));
        return cb.and(predicates.toArray(new Predicate[0]));
    };/*from ww  w  .j  ava 2s .  co  m*/
}

From source file:org.wallride.repository.PageSpecifications.java

public static Specification<Page> path(Page page, boolean includeUnpublished) {
    return (root, query, cb) -> {
        //         query.distinct(true);
        Root<Page> p1 = query.from(Page.class);
        Root<Page> p2 = root;/*from  www .j  ava  2  s  . c om*/
        Root<Page> p3 = query.from(Page.class);

        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(root.get(Page_.language), page.getLanguage()));
        if (!includeUnpublished) {
            predicates.add(cb.equal(root.get(Page_.status), Page.Status.PUBLISHED));
        }

        Subquery<Long> subquery = query.subquery(Long.class);
        Root<Page> p = subquery.from(Page.class);
        subquery.select(p.get(Page_.id)).where(cb.equal(p.get(Page_.language), page.getLanguage()),
                cb.isNull(p.get(Page_.parent)), cb.le(p.get(Page_.lft), page.getLft()),
                cb.ge(p.get(Page_.rgt), page.getRgt()));

        predicates.add(cb.equal(p1.get(Page_.id), subquery));
        predicates.add(cb.equal(p3.get(Page_.id), page.getId()));
        predicates.add(cb.between(p2.get(Page_.lft), p1.get(Page_.lft), p1.get(Page_.rgt)));
        predicates.add(cb.between(p3.get(Page_.lft), p2.get(Page_.lft), p2.get(Page_.rgt)));
        query.orderBy(cb.desc(cb.diff(p2.get(Page_.rgt), p2.get(Page_.lft))));
        return cb.and(predicates.toArray(new Predicate[0]));
    };
}

From source file:org.wallride.repository.CategorySpecifications.java

public static Specification<Category> hasArticle(String language) {
    return (root, query, cb) -> {
        query.distinct(true);/*w w  w  .  jav  a 2  s .c  om*/

        Subquery<Long> subquery = query.subquery(Long.class);
        Root<Article> a = subquery.from(Article.class);
        Join<Article, Category> c = a.join(Article_.categories, JoinType.INNER);
        subquery.select(c.get(Category_.id)).where(cb.equal(a.get(Article_.status), Article.Status.PUBLISHED));

        List<Predicate> predicates = new ArrayList<>();
        predicates.add(root.get(Category_.id).in(subquery));
        predicates.add(cb.equal(root.get(Category_.language), language));
        return cb.and(predicates.toArray(new Predicate[0]));
    };
}

From source file:com.goodhuddle.huddle.repository.MemberSpecification.java

public static Specification<Member> search(final Huddle huddle, final SearchMembersRequest request) {
    return new Specification<Member>() {
        @Override/* w ww.j  a  va 2s. co m*/
        public Predicate toPredicate(Root<Member> member, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Predicate conjunction = builder.conjunction();

            // huddle
            conjunction.getExpressions().add(builder.equal(member.get("huddle"), huddle));

            // keywords

            if (StringUtils.isNotBlank(request.getKeywords())) {
                String[] terms = StringUtils.split(request.getKeywords());
                for (String keyword : terms) {
                    if (keyword != null && keyword.length() > 0) {

                        String matchTerm = "%" + keyword.toLowerCase() + "%";
                        conjunction.getExpressions().add(builder.or(
                                builder.like(builder.lower(member.<String>get("username")), matchTerm),
                                builder.like(builder.lower(member.<String>get("email")), matchTerm),
                                builder.like(builder.lower(member.<String>get("firstName")), matchTerm),
                                builder.like(builder.lower(member.<String>get("lastName")), matchTerm)));
                    }
                }
            }

            // security groups

            if (CollectionUtils.isNotEmpty(request.getSecurityGroupIds())) {

                Join<Object, Object> securityGroup = member.join("securityGroup",
                        request.isIncludeNoAccess() ? JoinType.LEFT : JoinType.INNER);

                Predicate disjunction = builder.disjunction();
                for (Long id : request.getSecurityGroupIds()) {
                    disjunction.getExpressions().add(builder.equal(securityGroup.get("id"), id));
                }
                if (request.isIncludeNoAccess()) {
                    disjunction.getExpressions().add(builder.isNull(securityGroup.get("id")));
                }
                conjunction.getExpressions().add(disjunction);

            } else if (request.isIncludeNoAccess()) {

                conjunction.getExpressions().add(builder.isNull(member.get("securityGroup")));

            }

            // tags

            MemberTagFilter tagFilter = request.getTags();
            if (tagFilter != null) {

                if (tagFilter.getIncluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getIncluded().getTagIds())) {

                        MemberTagFilter.TagSet included = request.getTags().getIncluded();
                        MatchType matchType = included.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.conjunction()
                                : builder.disjunction();

                        for (Long tagId : included.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq));
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

                if (tagFilter.getExcluded() != null) {

                    if (CollectionUtils.isNotEmpty(tagFilter.getExcluded().getTagIds())) {

                        MemberTagFilter.TagSet excluded = request.getTags().getExcluded();
                        MatchType matchType = excluded.getMatchType();
                        Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.disjunction()
                                : builder.conjunction();

                        for (Long tagId : excluded.getTagIds()) {
                            Subquery<Member> sq = query.subquery(Member.class);
                            Root<Member> subMember = sq.from(Member.class);
                            Join<Member, Tag> tag = subMember.join("tags");
                            sq.select(subMember).where(builder.equal(tag.get("id"), tagId));
                            tagPredicate.getExpressions().add(builder.in(member).value(sq).not());
                        }

                        conjunction.getExpressions().add(tagPredicate);
                    }
                }

            }

            return conjunction;
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate existsInData(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String term) {// w w w.j ava  2  s  .co  m
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot)
            .where(cb.and(cb.equal(subroot.get(ANNO), root), cb.like(cb.lower(subroot.get(VALUE)), term))));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate hasAttributeName(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeName) {//from  ww  w  . j a  v  a 2  s  .  c  o  m
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(NAME)), attributeName.toLowerCase()))));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate hasAttributeLevel(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeLevel) {//  w  ww .java  2  s  .c  o m
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(LEVEL)), attributeLevel.toLowerCase()))));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate hasAttributeReqLevel(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String attributeReqLevel) {
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot).where(cb.and(cb.equal(subroot.get(ANNO), root),
            cb.like(cb.lower(subroot.get(ATTRIB).get(REQ_LEVEL)), attributeReqLevel.toLowerCase()))));
}

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);//ww  w  .  j  av  a2 s. c o  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);
}