List of usage examples for javax.persistence.criteria Root fetch
<Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute);
From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java
@Override @Transactional(readOnly = true)/*from w w w .j a v a2s . co m*/ public ExampleGeneratedIdEntity findWithOthersFetchJoin(final Long id) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder .createQuery(ExampleGeneratedIdEntity.class); final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass); root.fetch(ExampleGeneratedIdEntity_.otherEntities); criteriaQuery.where(builder.equal(root.get(ExampleGeneratedIdEntity_.id), id)); return this.findSingle(criteriaQuery); }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceGroupRepository.java
/** * * @param name/*from w w w .j av a2 s .com*/ * @param resourceTypeId * @return */ public ResourceGroupEntity loadUniqueGroupByNameAndType(String name, Integer resourceTypeId) { ResourceGroupEntity result = null; try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ResourceGroupEntity> q = cb.createQuery(ResourceGroupEntity.class); Root<ResourceGroupEntity> r = q.from(ResourceGroupEntity.class); r.fetch("resources"); Join<ResourceGroupEntity, ResourceEntity> resources = r.join("resources"); Predicate typePred = cb.equal(resources.get("resourceType").get("id"), resourceTypeId); Predicate resNamePred = cb.equal(resources.get("name"), name); q.where(cb.and(typePred, resNamePred)); q.distinct(true); result = entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { // do nothing } return result; }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional/*ww w .j a va 2s .com*/ public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
From source file:com.movies.dao.impl.BaseDaoImpl.java
@Override public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass, List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes, List<SetAttribute> setAttributes) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true); Root<T> root = cq.from(returnClass); if (CollectionUtils.isNotEmpty(singleAttributes)) { for (SingularAttribute attribute : singleAttributes) { root.join(attribute);//from w ww .ja v a 2s . c o m root.fetch(attribute); } } if (CollectionUtils.isNotEmpty(listAttributes)) { for (ListAttribute attribute : listAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } if (CollectionUtils.isNotEmpty(setAttributes)) { for (SetAttribute attribute : setAttributes) { root.join(attribute, JoinType.LEFT); root.fetch(attribute, JoinType.LEFT); } } Set<Entry<String, Object>> set = map.entrySet(); int numberOfClauses = set.size(); Predicate[] predicates = new Predicate[numberOfClauses]; int i = 0; for (Entry<String, Object> entry : set) { String key = entry.getKey(); if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) { predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE); } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) { //predicates[i++] = cb.equal( ,entry.getValue()); } else { predicates[i++] = cb.equal(root.get(key), entry.getValue()); } } return em.createQuery(cq.select(root).where(predicates)).getResultList(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * fetch join:/*w w w . j ava 2 s.com*/ * 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:de.whs.poodle.repositories.StatisticsRepository.java
public List<Statistic> getStatistics(FeedbackSearchCriteria s, int instructorId, int limit) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Statistic> cq = cb.createQuery(Statistic.class); Root<Statistic> statistic = cq.from(Statistic.class); /* fetch the exercises immediately so they don't have to * be lazy-fetched one by one while rendering the table rows. */ statistic.fetch("exercise"); // join we need to match the course Join<Statistic, Course> course = statistic.join("courseTerm").join("course"); // create empty "and" predicate Predicate where = cb.conjunction(); // only non-empty Predicate notEmpty = cb.isFalse(statistic.get("empty")); where = cb.and(where, notEmpty);/* w ww. ja v a 2 s . co m*/ // only statistics for courses that the instructor has access to Predicate hasInstructorAccessToCourse = cb.isTrue(cb.function("has_instructor_access_to_course", Boolean.class, course.get("id"), cb.literal(instructorId))); where = cb.and(where, hasInstructorAccessToCourse); // filter by courseId if (s.getCourseId() != 0) { Predicate courseMatches = cb.equal(course.get("id"), s.getCourseId()); where = cb.and(where, courseMatches); } // filter by student if (s.getStudent() != null) { Predicate studentMatches = cb.equal(statistic.get("student").get("id"), s.getStudent().getId()); where = cb.and(where, studentMatches); } cq.where(where); // order by date cq.orderBy(cb.desc(statistic.get("completedAt"))); return em.createQuery(cq).setMaxResults(limit).getResultList(); }
From source file:com.yunguchang.data.ApplicationRepository.java
public TBusApproveSugEntity getApplyApproveInfoByApplyNo(String applyNo) { if (applyNo == null) { throw new InvalidParameterException("Apply No can not be null!"); }/*from w w w. j a v a 2 s .c om*/ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class); Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class); approveRoot.fetch(TBusApproveSugEntity_.application); cq.where(cb.equal(approveRoot.get(TBusApproveSugEntity_.application).get(TBusApplyinfoEntity_.applyno), applyNo)); return Iterables.getFirst(em.createQuery(cq).getResultList(), null); }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApproveSugEntity> listApplyApproveInfo(DateTime startTime, DateTime endTime, PrincipalExt principalExtOrNull) { 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<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class); Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class); approveRoot.fetch(TBusApproveSugEntity_.application); approveRoot.fetch(TBusApproveSugEntity_.user, JoinType.LEFT); cq.select(approveRoot); Predicate predicate = cb.and( cb.between(approveRoot.get(TBusApproveSugEntity_.operatedate), new Timestamp(startTime.getMillis()), new Timestamp(endTime.getMillis())), cb.or(cb.isNull(approveRoot.get(TBusApproveSugEntity_.updateBySync)), cb.isFalse(approveRoot.get(TBusApproveSugEntity_.updateBySync)))); cq.where(predicate); TypedQuery<TBusApproveSugEntity> query = em.createQuery(cq); applySecurityFilter("applications", principalExtOrNull); return query.getResultList(); }
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 ww .ja v a 2s.c om*/ 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:com.yunguchang.data.ApplicationRepository.java
public List<TBusApplyinfoEntity> getAllApplications(String coordinatorUserId, String reasonType, String status, DateTime startBefore, DateTime startAfter, DateTime endBefore, DateTime endAfter, Integer offset, Integer limit, OrderByParam orderByParam, PrincipalExt principalExt) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class); Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class); applyRoot.fetch(TBusApplyinfoEntity_.passenger); Fetch<TBusApplyinfoEntity, TSysUserEntity> fetchCoordinator = applyRoot .fetch(TBusApplyinfoEntity_.coordinator); applyRoot.fetch(TBusApplyinfoEntity_.department); Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT); scheduleFetch.fetch(TBusScheduleRelaEntity_.senduser, JoinType.LEFT); scheduleFetch.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);//from ww w.j a v a 2 s. c o m fetchScheduleCar.fetch(TBusScheduleCarEntity_.driver, JoinType.LEFT).fetch(TRsDriverinfoEntity_.department, JoinType.LEFT); Predicate predicate = cb.conjunction(); if (coordinatorUserId != null) { Join<TBusApplyinfoEntity, TSysUserEntity> joinCoordinator = (Join<TBusApplyinfoEntity, TSysUserEntity>) fetchCoordinator; predicate = cb.and(predicate, cb.equal(joinCoordinator.get(TSysUserEntity_.userid), coordinatorUserId)); } if (reasonType != null) { predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.reason), reasonType)); } if (status != null) { if (status.indexOf(":") < 0) { predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.status), status)); } else { String[] statusList = status.split(":"); predicate = cb.and(predicate, applyRoot.get(TBusApplyinfoEntity_.status).in(Arrays.asList(statusList))); } } if (startBefore != null) { predicate = cb.and(predicate, cb.lessThanOrEqualTo(applyRoot.get(TBusApplyinfoEntity_.begintime), startBefore)); } if (startAfter != null) { predicate = cb.and(predicate, cb.greaterThanOrEqualTo(applyRoot.get(TBusApplyinfoEntity_.begintime), startAfter)); } if (endBefore != null) { predicate = cb.and(predicate, cb.lessThanOrEqualTo(applyRoot.get(TBusApplyinfoEntity_.endtime), endBefore)); } if (endAfter != null) { predicate = cb.and(predicate, cb.greaterThanOrEqualTo(applyRoot.get(TBusApplyinfoEntity_.endtime), endAfter)); } cq.where(predicate); List<Order> orders = new ArrayList<>(); if (orderByParam != null) { for (OrderByParam.OrderBy orderBy : orderByParam.getOrderBies()) { Order order = null; if (orderBy.getFiled().toLowerCase().equals("start".toLowerCase())) { order = cb.desc(applyRoot.get(TBusApplyinfoEntity_.begintime)); } if (order != null && !orderBy.isAsc()) { order = order.reverse(); } if (order != null) { orders.add(order); } } } if (orders.size() == 0) { cq.orderBy(cb.desc(applyRoot.get(TBusApplyinfoEntity_.begintime))); } else { cq.orderBy(orders); } TypedQuery<TBusApplyinfoEntity> query = em.createQuery(cq); if (offset != null) { query.setFirstResult(offset); } if (limit != null) { query.setMaxResults(limit); } applySecurityFilter("applications", principalExt); return query.getResultList(); }