Example usage for javax.persistence.criteria JoinType LEFT

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

Introduction

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

Prototype

JoinType LEFT

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

Click Source Link

Document

Left outer join.

Usage

From source file:com.expressui.sample.view.account.AccountQuery.java

@Override
public void addFetchJoins(Root<Account> rootEntity) {
    rootEntity.fetch("billingAddress", JoinType.LEFT);
}

From source file:com.expressui.sample.view.contact.ContactQuery.java

@Override
public void addFetchJoins(Root<Contact> rootEntity) {
    rootEntity.fetch("mailingAddress", JoinType.LEFT).fetch("state", JoinType.LEFT);
    rootEntity.fetch("account", JoinType.LEFT);
}

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

@Test
public void resolvesJoinFetchEvenIfOtherSpecificationIsNotPresent() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);

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

    assertThat(resolved).isEqualTo(new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(
            new String[] { "fetch1", "fetch2" }, JoinType.LEFT));
}

From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java

public List<ServerTuple> getNodeServers(String hostFilter, String appServerFilter, String runtimeFilter,
        String nodeFilter, String contextFilter) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class);

    //get Node ResType
    Root<ResourceEntity> node = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT);

    //get Props on node
    Join<ResourceEntity, ResourceContextEntity> resCtx = node.join("contexts", JoinType.LEFT);
    Join<ResourceContextEntity, ContextEntity> nodeCtx = resCtx.join("context", JoinType.LEFT);
    Join<ContextEntity, ContextEntity> nodeDomain = nodeCtx.join("parent", JoinType.LEFT);
    Join<ResourceContextEntity, PropertyEntity> nodeProp = resCtx.join("properties", JoinType.LEFT);
    Join<PropertyEntity, PropertyDescriptorEntity> nodePropType = nodeProp.join("descriptor", JoinType.LEFT);

    //get AppServer
    Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations",
            JoinType.LEFT);//from w w w . j a va  2 s .  co  m
    Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource",
            JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT);

    //get Runtime of as
    Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations",
            JoinType.LEFT);
    Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource",
            JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT);

    q.select(cb.construct(ServerTuple.class, nodeProp.get("value"), appServer.get("name"), appServer.get("id"),
            asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"),
            nodeRelease.get("name"), nodeDomain.get("name"), nodeDomain.get("id"), nodeCtx.get("name"),
            nodeCtx.get("id"), cb.literal(1) // true
    ));

    Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()),
            cb.or(cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()),
                    cb.isNull(asType.get("name")) //nodes without appServer
            ),
            cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME),
                    cb.isNull(runtimeType.get("name"))),
            cb.isNotNull(nodeProp.get("value")), cb.equal(nodePropType.get("propertyName"), "hostName")

    );

    p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter,
            nodeProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"),
            node.<String>get("name"), nodeCtx.<String>get("name"));

    q.where(p);

    TypedQuery<ServerTuple> query = entityManager.createQuery(q);
    List<ServerTuple> servers = query.getResultList();

    return servers;
}

From source file:org.querybyexample.jpa.GenericRepository.java

/**
 * Find and load a list of E instance.//from w  w  w  .jav a 2  s  .c o  m
 *
 * @param entity a sample entity whose non-null properties may be used as
 * search hints
 * @param searchParameters carries additional search information
 * @return the entities matching the search.
 */
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<E> find(E entity, SearchParameters sp) {
    if (sp.hasNamedQuery()) {
        return byNamedQueryUtil.findByNamedQuery(sp);
    }
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<E> criteriaQuery = builder.createQuery(type);
    if (sp.getDistinct()) {
        criteriaQuery.distinct(true);
    }
    Root<E> root = criteriaQuery.from(type);

    // predicate
    Predicate predicate = getPredicate(root, builder, entity, sp);
    if (predicate != null) {
        criteriaQuery = criteriaQuery.where(predicate);
    }

    // left join
    for (SingularAttribute<?, ?> arg : sp.getLeftJoins()) {
        root.fetch((SingularAttribute<E, ?>) arg, JoinType.LEFT);
    }

    // order by
    criteriaQuery.orderBy(orderByUtil.buildJpaOrders(sp.getOrders(), root, builder, sp));

    TypedQuery<E> typedQuery = entityManager.createQuery(criteriaQuery);
    applyCacheHints(typedQuery, sp);
    applyPagination(typedQuery, sp);
    List<E> entities = typedQuery.getResultList();
    log.debug("Returned {} elements", entities.size());

    return entities;
}

From source file:com.dbs.sdwt.jpa.JpaUtil.java

@SuppressWarnings("unchecked")
public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) {
    Path<?> path = root;/*from w  ww  . j  a  v a  2s  .c  o m*/
    for (Attribute<?, ?> attribute : attributes) {
        boolean found = false;
        if (path instanceof FetchParent) {
            for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) {
                if (attribute.getName().equals(fetch.getAttribute().getName())
                        && (fetch instanceof Join<?, ?>)) {
                    path = (Join<E, ?>) fetch;
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            if (attribute instanceof PluralAttribute) {
                path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT);
            } else {
                path = path.get(attribute.getName());
            }
        }
    }
    return (Path<F>) path;
}

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

/**
 * out join//  w  ww  .  j a  va 2s  .  co  m
 */
@Transactional
public void doFrom2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Employee> e = c.from(Employee.class);
    Join<Object, Object> address = e.join("address", JoinType.LEFT);
    Join<Object, Object> department = e.join("department", JoinType.LEFT);
    c.multiselect(e, address, department);

    showResult(c);
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TBusApplyinfoEntity> getApplicationForSync(DateTime startTime, DateTime endTime, String status,
        Boolean isSend, PrincipalExt principalExt) {
    if (startTime == null) {
        startTime = DateTimeUtil.appStartTime;
    }//  w w w  .  j  ava  2s  . c  o m
    if (endTime == null) {
        endTime = DateTimeUtil.appEndTime;
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    applyRoot.fetch(TBusApplyinfoEntity_.passenger);
    applyRoot.fetch(TBusApplyinfoEntity_.coordinator);
    applyRoot.fetch(TBusApplyinfoEntity_.department);
    applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT);

    Predicate predicate = cb.conjunction();

    predicate = cb.and(predicate, cb.or(cb.isNull(applyRoot.get(TBusApplyinfoEntity_.updateBySync)),
            cb.isFalse(applyRoot.get(TBusApplyinfoEntity_.updateBySync))));
    if (status != null) {
        predicate = cb.and(predicate,
                // "2" ->  "3" -> ? "4" -> ?
                cb.equal(applyRoot.get(TBusApplyinfoEntity_.status), status));
    }
    if (isSend != null && isSend) {
        predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.issend), "1") // 
        );
    }
    if ("4".equals(status)) {
        Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot
                .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT);
        Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch;
        scheduleJoin.fetch(TBusScheduleRelaEntity_.reciveuser, JoinType.LEFT);
        Fetch<TBusScheduleRelaEntity, TBusScheduleCarEntity> fetchScheduleCar = scheduleFetch
                .fetch(TBusScheduleRelaEntity_.scheduleCars, JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.car, JoinType.LEFT).fetch(TAzCarinfoEntity_.depot,
                JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.driver, JoinType.LEFT)
                .fetch(TRsDriverinfoEntity_.department, JoinType.LEFT);
        predicate = cb.and(predicate,
                cb.or(cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime),
                        cb.between(scheduleJoin.get(TBusScheduleRelaEntity_.updatedate), startTime, endTime)));
    } else {
        predicate = cb.and(predicate,
                cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime));
    }

    cq.where(predicate);

    TypedQuery<TBusApplyinfoEntity> query = em.createQuery(cq);

    //        applySecurityFilter("applications", principalExt);
    return query.getResultList();
}

From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java

public List<ServerTuple> getAppServers(String hostFilter, String appServerFilter, String runtimeFilter,
        String nodeFilter, String contextFilter) {

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class);

    //get Node ResType
    Root<ResourceEntity> node = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT);

    //get AppServer
    Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations",
            JoinType.LEFT);/*  w  w  w  . j a  v a2  s  .co m*/
    Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource",
            JoinType.LEFT);
    Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT);

    //get Runtime of as
    Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations",
            JoinType.LEFT);
    Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource",
            JoinType.LEFT);
    Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT);

    //get Props between as and node
    Join<ConsumedResourceRelationEntity, ResourceRelationContextEntity> resRelCtx = nodeResRel.join("contexts",
            JoinType.LEFT);
    Join<ResourceRelationContextEntity, ContextEntity> asCtx = resRelCtx.join("context", JoinType.LEFT);
    Join<ContextEntity, ContextEntity> asDomain = asCtx.join("parent", JoinType.LEFT);

    Join<ResourceRelationContextEntity, PropertyEntity> asProp = resRelCtx.join("properties", JoinType.LEFT);
    //here an on clause should be added, so we don't get hostnames that are divined directly on the node multiple times (and descriptor.propertyName = 'hostName')
    //on support was added in jpa 2.1 which is part of JEE 7
    Join<PropertyEntity, PropertyDescriptorEntity> asPropType = asProp.join("descriptor", JoinType.LEFT);

    q.select(cb.construct(ServerTuple.class, asProp.get("value"), appServer.get("name"), appServer.get("id"),
            asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"),
            nodeRelease.get("name"), asDomain.get("name"), asDomain.get("id"), asCtx.get("name"),
            asCtx.get("id"), cb.literal(0) //false
    ));

    Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()),
            cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()),
            cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME),
                    cb.isNull(runtimeType.get("name"))),
            cb.isNotNull(asProp.get("value")), cb.equal(asPropType.get("propertyName"), "hostName"));

    p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter,
            asProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"),
            node.<String>get("name"), asCtx.<String>get("name"));

    q.where(p);

    TypedQuery<ServerTuple> query = entityManager.createQuery(q);
    List<ServerTuple> servers = query.getResultList();

    return servers;
}

From source file:dk.dma.msinm.service.CategoryService.java

/**
 * Looks up an category by name//from  w w  w  .j av a  2  s  .  co  m
 * @param name the name to search for
 * @param lang the language. Optional
 * @param parentId the parent ID. Optional
 * @return The matching category, or null if not found
 */
public Category findByName(String name, String lang, Integer parentId) {
    // Sanity check
    if (StringUtils.isBlank(name)) {
        return null;
    }

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Category> categoryQuery = builder.createQuery(Category.class);

    Root<Category> categoryRoot = categoryQuery.from(Category.class);

    // Build the predicate
    PredicateHelper<Category> predicateBuilder = new PredicateHelper<>(builder, categoryQuery);

    // Match the name
    Join<Category, CategoryDesc> descs = categoryRoot.join("descs", JoinType.LEFT);
    predicateBuilder.like(descs.get("name"), name);
    // Optionally, match the language
    if (StringUtils.isNotBlank(lang)) {
        predicateBuilder.equals(descs.get("lang"), lang);
    }

    // Optionally, match the parent
    if (parentId != null) {
        categoryRoot.join("parent", JoinType.LEFT);
        Path<Category> parent = categoryRoot.get("parent");
        predicateBuilder.equals(parent.get("id"), parentId);
    }

    // Complete the query
    categoryQuery.select(categoryRoot).distinct(true).where(predicateBuilder.where());

    // Execute the query and update the search result
    List<Category> result = em.createQuery(categoryQuery).getResultList();

    return result.size() > 0 ? result.get(0) : null;
}