List of usage examples for javax.persistence.criteria CriteriaBuilder conjunction
Predicate conjunction();
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 w ww .j av a 2 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: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 ww w . ja v a 2 s . co m return em.createQuery(cq).getResultList(); }
From source file:com.it.j2ee.modules.common.util.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, // ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); }// w ww.j a v a2 s .c o m // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case IN: CriteriaBuilder.In in = builder.in(expression); String values = (String) filter.value; String[] value = values.split(","); for (int i = 0; i < value.length; i++) { in.value(value[i]); } predicates.add(in); break; case NEQ: predicates.add(builder.notEqual(expression, (Comparable) filter.value)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApplyinfoEntity> getApplicationWithDetailByScheduleId(String scheduleId, PrincipalExt principalExt) {/* w w w . java 2 s . c o 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(); }
From source file:net.shopxx.dao.impl.StatisticDaoImpl.java
public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) { Assert.notNull(period);//from w w w .j a v a 2 s . co m CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class); Root<Statistic> root = criteriaQuery.from(Statistic.class); switch (period) { case year: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year")); break; case month: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year"), root.get("month")); break; case day: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"), root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"), root.<BigDecimal>get("completeOrderAmount"))); break; } Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { Calendar calendar = DateUtils.toCalendar(beginDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.greaterThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day)))); } if (endDate != null) { Calendar calendar = DateUtils.toCalendar(endDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.lessThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day)))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:com.faceye.feature.repository.jpa.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//from w ww .j a va 2s . co m public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (CollectionUtils.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // logic operator switch (filter.operator) { case EQ: if (filter.value instanceof Number) { predicates.add(builder.equal(expression, (Number) filter.value)); } else if (filter.value instanceof String) { if (StringUtils.isNotEmpty(filter.value.toString())) { predicates.add(builder.equal(expression, filter.value)); } } else { predicates.add(builder.equal(expression, filter.value)); } break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ISTRUE: predicates.add(builder.isTrue(expression)); break; case ISFALSE: predicates.add(builder.isFalse(expression)); break; case ISEMPTY: predicates.add(builder.isEmpty(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case NE: predicates.add(builder.notEqual(expression, filter.value)); } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
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);// www.j a va2s .c om 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(); }
From source file:cn.imethan.common.repository.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//w w w .j a v a2s .c o m public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (filters != null && !filters.isEmpty()) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); try { for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } } catch (Exception e) { e.printStackTrace(); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case IN: In in = builder.in(expression); String[] valueStrings = StringUtils.split(filter.value.toString(), ","); List<Long> list = new ArrayList<Long>(); for (String value : valueStrings) { list.add(Long.valueOf(value.trim())); } in.value(list); predicates.add(in); break; case NOTIN: In in1 = builder.in(expression); String[] valueStrings1 = StringUtils.split(filter.value.toString(), ","); List<Long> list1 = new ArrayList<Long>(); for (String value : valueStrings1) { list1.add(Long.valueOf(value.trim())); } in1.value(list1); predicates.add(builder.not(in1)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } // //hibernate // //org.hibernate.ejb.criteria.CriteriaQueryImpl // if(query instanceof org.hibernate.ejb.QueryImpl) { // @SuppressWarnings("rawtypes") // org.hibernate.ejb.QueryImpl hibernateQuery = (org.hibernate.ejb.QueryImpl)query; // org.hibernate.Query hQuery = hibernateQuery.getHibernateQuery(); // hQuery.setCacheable(true); // } return builder.conjunction(); } }; }
From source file:aode.lx.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/* www .j a va 2 s .c o m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); List<Predicate> ORpredicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } Object value = filter.value; if (expression.getJavaType().isEnum()) { value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString()); predicates.add(builder.equal(expression, value)); } else { // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case NEQ: predicates.add(builder.notEqual(expression, filter.value)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case OREQ: ORpredicates.add(builder.equal(expression, filter.value)); break; case ORLIKE: ORpredicates.add(builder.like(expression, "%" + filter.value + "%")); break; case ORGT: ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case ORLT: ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case ORGTE: ORpredicates .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLTE: ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORNEQ: ORpredicates.add(builder.notEqual(expression, filter.value)); break; case ORNOTNULL: ORpredicates.add(builder.isNotNull(expression)); break; case ORISNULL: ORpredicates.add(builder.isNull(expression)); break; } } } // ? and ??? if (!predicates.isEmpty() && !ORpredicates.isEmpty()) { return builder.and(builder.and(predicates.toArray(new Predicate[predicates.size()])), builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()]))); } if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } if (!ORpredicates.isEmpty()) { return builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()])); } } return builder.conjunction(); } }; }