Example usage for javax.persistence.criteria Join fetch

List of usage examples for javax.persistence.criteria Join fetch

Introduction

In this page you can find the example usage for javax.persistence.criteria Join fetch.

Prototype

<Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt);

Source Link

Document

Create a fetch join to the specified single-valued attribute using the given join type.

Usage

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;
    }/*from  ww w  .j  a  v a2 s  .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:org.batoo.jpa.benchmark.BenchmarkTest.java

private void test(final EntityManagerFactory emf, Queue<Runnable> workQueue, int length) {
    final CriteriaBuilder cb = emf.getCriteriaBuilder();
    final CriteriaQuery<Address> cq = cb.createQuery(Address.class);

    final Root<Person> r = cq.from(Person.class);
    final Join<Person, Address> a = r.join("addresses");
    a.fetch("country", JoinType.LEFT);
    a.fetch("person", JoinType.LEFT);
    cq.select(a);/* w  w  w  .  j  av  a 2s . c o m*/

    final ParameterExpression<Person> p = cb.parameter(Person.class);
    cq.where(cb.equal(r, p));

    for (int i = 0; i < length; i++) {
        workQueue.add(new Runnable() {

            @Override
            public void run() {
                try {
                    BenchmarkTest.this.singleTest(emf, BenchmarkTest.this.createPersons(), cq, p);
                } catch (final Exception e) {
                    BenchmarkTest.LOG.error(e, "Error while running the test");
                }
            }
        });
    }
}