Example usage for javax.persistence.criteria JoinType INNER

List of usage examples for javax.persistence.criteria JoinType INNER

Introduction

In this page you can find the example usage for javax.persistence.criteria JoinType INNER.

Prototype

JoinType INNER

To view the source code for javax.persistence.criteria JoinType INNER.

Click Source Link

Document

Inner join.

Usage

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

public static Specification<Category> hasArticle(String language) {
    return (root, query, cb) -> {
        query.distinct(true);//ww  w.j av a 2 s .com

        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//from  w  w  w.j a v a2  s .  c om
        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:com.expressui.core.dao.security.query.RelatedRolesQuery.java

@Override
public void addFetchJoins(Root<Role> role) {
    role.fetch("userRoles", JoinType.INNER).fetch("user", JoinType.INNER);
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolverTest.java

@Test
public void resolvesJoinContainer() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod_joinContainer"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isInstanceOf(Conjunction.class);

    Collection<Specification<?>> innerSpecs = ReflectionUtils.get(resolved, "innerSpecs");

    assertThat(innerSpecs).hasSize(2).contains(new Like<Object>("path1", new String[] { "value1" }))
            .contains(new Conjunction<Object>(
                    new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1" },
                            JoinType.LEFT),
                    new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch2" },
                            JoinType.INNER)));
}

From source file:com.expressui.core.dao.security.query.RelatedUsersQuery.java

@Override
public void addFetchJoins(Root<User> user) {
    user.fetch("userRoles", JoinType.INNER).fetch("user", JoinType.INNER);
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * fetch join:/*from  w w  w .j  av a  2  s .c  o  m*/
 * SELECT e FROM jpa_query_employee e JOIN FETCH e.address
 */
//   @Transactional
public void doFrom3() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);
    e.fetch("address");
    e.fetch("department");
    e.fetch("projects", JoinType.INNER);
    //      e.fetch("phones");
    //      e.fetch("directs");
    //      e.fetch("manager");
    c.select(e);

    // only show the fetched data
    TypedQuery<Employee> query = em.createQuery(c);
    List<Employee> result = query.getResultList();
    for (Employee emp : result) {
        System.out.println(emp.getId() + " | " + emp.getName() + " | " + emp.getAddress() + " | "
                + emp.getDepartment() + " | " + emp.getProjects());
    }
}

From source file:net.sf.companymanager.qbe.SearchParameters.java

/**
 * Returns the attribute (x-to-one association) which must be fetched with
 * an inner join.//from  w  ww.ja v  a  2 s.  c o  m
 * @return List
 */
public List<SingularAttribute<?, ?>> getInnerJoinAttributes() {
    return getJoinAttributes(JoinType.INNER);
}

From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java

private Predicate createCompositeParamPart(CriteriaBuilder builder, Root<ResourceTable> from,
        RuntimeSearchParam left, IQueryParameterType leftValue) {
    Predicate retVal = null;/*from  ww  w.ja v  a2 s  .c  o m*/
    switch (left.getParamType()) {
    case STRING: {
        From<ResourceIndexedSearchParamString, ResourceIndexedSearchParamString> stringJoin = from
                .join("myParamsString", JoinType.INNER);
        retVal = createPredicateString(leftValue, left.getName(), builder, stringJoin);
        break;
    }
    case TOKEN: {
        From<ResourceIndexedSearchParamToken, ResourceIndexedSearchParamToken> tokenJoin = from
                .join("myParamsToken", JoinType.INNER);
        retVal = createPredicateToken(leftValue, left.getName(), builder, tokenJoin);
        break;
    }
    case DATE: {
        From<ResourceIndexedSearchParamDate, ResourceIndexedSearchParamDate> dateJoin = from
                .join("myParamsDate", JoinType.INNER);
        retVal = createPredicateDate(builder, dateJoin, leftValue);
        break;
    }
    }

    if (retVal == null) {
        throw new InvalidRequestException(
                "Don't know how to handle composite parameter with type of " + left.getParamType());
    }

    return retVal;
}

From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java

private void createSort(CriteriaBuilder theBuilder, Root<ResourceTable> theFrom, SortSpec theSort,
        List<Order> theOrders, List<Predicate> thePredicates) {
    if (theSort == null || isBlank(theSort.getParamName())) {
        return;//from ww w  .j  av a  2s .  c  om
    }

    if ("_id".equals(theSort.getParamName())) {
        From<?, ?> forcedIdJoin = theFrom.join("myForcedId", JoinType.LEFT);
        if (theSort.getOrder() == null || theSort.getOrder() == SortOrderEnum.ASC) {
            theOrders.add(theBuilder.asc(forcedIdJoin.get("myForcedId")));
            theOrders.add(theBuilder.asc(theFrom.get("myId")));
        } else {
            theOrders.add(theBuilder.desc(forcedIdJoin.get("myForcedId")));
            theOrders.add(theBuilder.desc(theFrom.get("myId")));
        }

        createSort(theBuilder, theFrom, theSort.getChain(), theOrders, null);
        return;
    }

    RuntimeResourceDefinition resourceDef = getContext().getResourceDefinition(myResourceType);
    RuntimeSearchParam param = resourceDef.getSearchParam(theSort.getParamName());
    if (param == null) {
        throw new InvalidRequestException("Unknown sort parameter '" + theSort.getParamName() + "'");
    }

    String joinAttrName;
    String sortAttrName;

    switch (param.getParamType()) {
    case STRING:
        joinAttrName = "myParamsString";
        sortAttrName = "myValueExact";
        break;
    case DATE:
        joinAttrName = "myParamsDate";
        sortAttrName = "myValueLow";
        break;
    default:
        throw new NotImplementedException("This server does not support _sort specifications of type "
                + param.getParamType() + " - Can't serve _sort=" + theSort.getParamName());
    }

    From<?, ?> stringJoin = theFrom.join(joinAttrName, JoinType.INNER);
    // Predicate p = theBuilder.equal(stringJoin.get("myParamName"), theSort.getParamName());
    // Predicate pn = theBuilder.isNull(stringJoin.get("myParamName"));
    // thePredicates.add(theBuilder.or(p, pn));

    if (theSort.getOrder() == null || theSort.getOrder() == SortOrderEnum.ASC) {
        theOrders.add(theBuilder.asc(stringJoin.get(sortAttrName)));
    } else {
        theOrders.add(theBuilder.desc(stringJoin.get(sortAttrName)));
    }

    createSort(theBuilder, theFrom, theSort.getChain(), theOrders, null);
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private Predicate createCompositeParamPart(CriteriaBuilder builder, Root<ResourceTable> from,
        RuntimeSearchParam left, IQueryParameterType leftValue) {
    Predicate retVal = null;/*from   ww  w.  j av a2 s . c  om*/
    switch (left.getParamType()) {
    case STRING: {
        From<ResourceIndexedSearchParamString, ResourceIndexedSearchParamString> stringJoin = from
                .join("myParamsString", JoinType.INNER);
        retVal = createPredicateString(leftValue, left.getName(), builder, stringJoin);
        break;
    }
    case TOKEN: {
        From<ResourceIndexedSearchParamToken, ResourceIndexedSearchParamToken> tokenJoin = from
                .join("myParamsToken", JoinType.INNER);
        retVal = createPredicateToken(leftValue, left.getName(), builder, tokenJoin);
        break;
    }
    case DATE: {
        From<ResourceIndexedSearchParamDate, ResourceIndexedSearchParamDate> dateJoin = from
                .join("myParamsDate", JoinType.INNER);
        retVal = createPredicateDate(builder, dateJoin, leftValue);
        break;
    }
    case QUANTITY: {
        From<ResourceIndexedSearchParamQuantity, ResourceIndexedSearchParamQuantity> dateJoin = from
                .join("myParamsQuantity", JoinType.INNER);
        retVal = createPredicateQuantity(builder, dateJoin, leftValue);
        break;
    }
    }

    if (retVal == null) {
        throw new InvalidRequestException(
                "Don't know how to handle composite parameter with type of " + left.getParamType());
    }

    return retVal;
}