List of usage examples for javax.persistence.criteria Root fetch
<Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt);
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:com.home.ln_spring.ch10.service.jpa.ContactServiceImplOld.java
@Override @Transactional(readOnly = true)// w ww . j a v a 2 s.co m public List<Contact> findByCriteriaQuery(String firstName, String lastName) { log.info("Finding contact for firstName: " + firstName + " and lastName: " + lastName); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Contact> criteriaQuery = cb.createQuery(Contact.class); Root<Contact> contactRoot = criteriaQuery.from(Contact.class); contactRoot.fetch(Contact_.contactTelDetails, JoinType.LEFT); contactRoot.fetch(Contact_.hobbies, JoinType.LEFT); criteriaQuery.select(contactRoot).distinct(true); Predicate criteria = cb.conjunction(); if (firstName != null) { Predicate p = cb.equal(contactRoot.get(Contact_.firstName), firstName); criteria = cb.and(criteria, p); } if (lastName != null) { Predicate p = cb.equal(contactRoot.get(Contact_.lastName), lastName); criteria = cb.and(criteria, p); } criteriaQuery.where(criteria); List<Contact> result = em.createQuery(criteriaQuery).getResultList(); return result; }
From source file:se.inera.intyg.intygstjanst.persistence.model.dao.impl.CertificateDaoImpl.java
@Override public List<Certificate> findCertificate(Personnummer civicRegistrationNumber, List<String> types, LocalDate fromDate, LocalDate toDate, List<String> careUnits) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Certificate> query = criteriaBuilder.createQuery(Certificate.class); Root<Certificate> root = query.from(Certificate.class); root.fetch("states", JoinType.LEFT); if (civicRegistrationNumber == null) { return Collections.emptyList(); }/*from w w w. j a v a2 s . c om*/ List<Predicate> predicates = new ArrayList<>(); // meta data has to match civic registration number predicates.add(criteriaBuilder.equal(root.get("civicRegistrationNumber"), DaoUtil.formatPnrForPersistence(civicRegistrationNumber))); // filter by certificate types if (types != null && !types.isEmpty()) { predicates.add(criteriaBuilder.lower(root.<String>get("type")).in(toLowerCase(types))); } // filter by care unit if (careUnits != null && !careUnits.isEmpty()) { predicates.add(root.<String>get("careUnitId").in(careUnits)); } query.where(predicates.toArray(new Predicate[predicates.size()])); // order by signed date query.orderBy(criteriaBuilder.asc(root.get("signedDate"))); List<Certificate> tmpResult = entityManager.createQuery(query).getResultList(); List<Certificate> result = filterDuplicates(tmpResult); // expect a small number, so lets filter in memory return new DateFilter(result).filter(fromDate, toDate); }
From source file:net.kaczmarzyk.spring.data.jpa.domain.JoinFetch.java
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { if (!Number.class.isAssignableFrom(query.getResultType())) { // do not join in count queries for (String path : pathsToFetch) { root.fetch(path, joinType); }/*from w w w.ja va 2s. co m*/ } return null; }
From source file:org.querybyexample.jpa.GenericRepository.java
/** * Find and load a list of E instance./*from w w w . ja v a2 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.yunguchang.data.ApplicationRepository.java
public TBusApplyinfoEntity getApplicationById(String id, PrincipalExt principalExt) { applySecurityFilter("applications", principalExt); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class); Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class); applyRoot.fetch(TBusApplyinfoEntity_.passenger, JoinType.LEFT); applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT); cq.where(cb.equal(applyRoot.get(TBusApplyinfoEntity_.uuid), id)); return Iterables.getFirst(em.createQuery(cq).getResultList(), null); }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TAzCarinfoEntity> listAllCandidateCars(String[] applicationIds, String keyword, Integer offset, Integer limit, PrincipalExt principalExt) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TAzCarinfoEntity> cq = cb.createQuery(TAzCarinfoEntity.class); Root<TAzCarinfoEntity> carRoot = cq.from(TAzCarinfoEntity.class); carRoot.fetch(TAzCarinfoEntity_.driver, JoinType.LEFT); cq.select(carRoot);//from w w w. ja v a 2 s .c o m Subquery<TBusScheduleCarEntity> overlapScheduleCarSubQuery = cq.subquery(TBusScheduleCarEntity.class); applyOverlapScheduleCarSubquery(overlapScheduleCarSubQuery, applicationIds, carRoot, null, cb, principalExt); Predicate predicate = cb.and(cb.equal(carRoot.get(TAzCarinfoEntity_.clzt), "02"), cb.or(cb.equal(carRoot.get(TAzCarinfoEntity_.repairingState), RepairingState.NONE.id()), cb.isNull(carRoot.get(TAzCarinfoEntity_.repairingState)) ), cb.not(cb.exists(overlapScheduleCarSubQuery)) ); if (keyword != null) { predicate = cb.and(predicate, cb.or(cb.like(carRoot.get(TAzCarinfoEntity_.sysOrg).get(TSysOrgEntity_.orgname), "%" + keyword + "%"), cb.like(carRoot.get(TAzCarinfoEntity_.cphm), "%" + keyword + "%"))); } cq.where(predicate); cq.orderBy(cb.asc(carRoot.get(TAzCarinfoEntity_.cphm))); TypedQuery<TAzCarinfoEntity> query = em.createQuery(cq); if (offset != null) { query.setFirstResult(offset); } if (limit != null) { query.setMaxResults(limit); } applySecurityFilter("cars", principalExt); return query.getResultList(); }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApplyinfoEntity> getApplicationByScheduleId(String scheduleId, PrincipalExt principalExt) { applySecurityFilter("applications", principalExt); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class); Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class); Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT); Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch; Predicate predicate = cb.conjunction(); predicate = cb.and(predicate, cb.equal(scheduleJoin.get(TBusScheduleRelaEntity_.uuid), scheduleId)); cq.where(predicate);/*from www.j av a 2 s . c o m*/ return em.createQuery(cq).getResultList(); }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApplyinfoEntity> getApplicationWithDetailByScheduleId(String scheduleId, PrincipalExt principalExt) {//from ww w . ja v a2 s. co m applySecurityFilter("applications", principalExt); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class); Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class); Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT); applyRoot.fetch(TBusApplyinfoEntity_.fleet); applyRoot.fetch(TBusApplyinfoEntity_.passenger); applyRoot.fetch(TBusApplyinfoEntity_.coordinator); applyRoot.fetch(TBusApplyinfoEntity_.department); Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch; Predicate predicate = cb.conjunction(); predicate = cb.and(predicate, cb.equal(scheduleJoin.get(TBusScheduleRelaEntity_.uuid), scheduleId)); cq.where(predicate); return em.createQuery(cq).getResultList(); }