List of usage examples for javax.persistence.criteria CriteriaBuilder lessThanOrEqualTo
<Y extends Comparable<? super Y>> Predicate lessThanOrEqualTo(Expression<? extends Y> x, Y y);
From source file:com.hengyi.japp.execution.Util.java
public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<PayOrder> root, PayOrderQueryCommand command) {/*from ww w. j a v a 2s. c o m*/ List<Predicate> ps = Lists.newArrayListWithCapacity(3); if (command.getCustomer() != null) { ps.add(cb.equal(root.get(PayOrder_.customer), command.getCustomer())); } if (command.getPayDateStart() != null) { ps.add(cb.greaterThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart())); } if (command.getPayDateEnd() != null) { ps.add(cb.lessThanOrEqualTo(root.get(PayOrder_.payDate), command.getPayDateStart())); } cq.where(ps.toArray(new Predicate[ps.size()])); }
From source file:br.nom.abdon.gastoso.dal.FatosDao.java
protected static void buildQuery(final CriteriaBuilder cb, final FiltroFatos filtroFatos, final Path<Fato> fatoPath, final List<Predicate> where, final Map<String, Object> params) { final Fato fato = filtroFatos.getFato(); if (fato != null) { final ParameterExpression<Fato> fatoParameter = cb.parameter(Fato.class, "fato"); where.add(cb.equal(fatoParameter, fatoPath)); params.put("fato", fato); }/*from w w w . java2 s. co m*/ final LocalDate dataMaxima = filtroFatos.getDataMaxima(); if (dataMaxima != null) { final Path<LocalDate> diaPath = fatoPath.get("dia"); final ParameterExpression<LocalDate> dataMaximaParameter = cb.parameter(LocalDate.class, "dataMaxima"); final Predicate menorOuIgualDataMaxima = cb.lessThanOrEqualTo(diaPath, dataMaximaParameter); where.add(menorOuIgualDataMaxima); params.put("dataMaxima", dataMaxima); } final LocalDate dataMinima = filtroFatos.getDataMinima(); if (dataMinima != null) { final Path<LocalDate> diaPath = fatoPath.get("dia"); final ParameterExpression<LocalDate> dataMinimaParameter = cb.parameter(LocalDate.class, "dataMinima"); final Predicate maiorOuIgualQueDataMinima = cb.greaterThanOrEqualTo(diaPath, dataMinimaParameter); where.add(maiorOuIgualQueDataMinima); params.put("dataMinima", dataMinima); } }
From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java
@Override public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng, EventType... eventTypes) {/*from w w w.j ava 2 s . com*/ CriteriaBuilder criteriaBuilder = getCriteriaBuilder(); CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class); Root<EventEntity> root = criteriaQuery.from(EventEntity.class); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat)); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat)); predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng)); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng)); if (ArrayUtils.isNotEmpty(eventTypes)) { List<Predicate> eventTypePredicates = new ArrayList<Predicate>(); for (EventType et : eventTypes) { eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et)); } predicates.add(criteriaBuilder.and( criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()])))); } criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])); return getAllByCriteria(criteriaQuery); }
From source file:com.wms.studio.service.LoginIpServiceImpl.java
@Override @org.springframework.transaction.annotation.Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<LoginIpInfoDto> findBy(final String userId, final Date start, final Date end, PageSize pageSize) {/* ww w . j a v a2 s .c om*/ if (pageSize == null) { pageSize = new PageSize(); } return loginIpConvert.covertToDto(loginIpRepository.findAll(new Specification<LoginIp>() { @Override public Predicate toPredicate(Root<LoginIp> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<>(3); if (start != null) { pres.add(cb.greaterThanOrEqualTo(root.get("loginTime").as(Date.class), start)); } if (end != null) { pres.add(cb.lessThanOrEqualTo(root.get("loginTime").as(Date.class), end)); } if (!StringUtils.isBlank(userId)) { pres.add(cb.equal(root.get("user").as(User.class), new User(userId))); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit()))); }
From source file:com.moderndrummer.data.MemberDaoImpl.java
@Override public List<Member> findAllCreatedMembersByFromAndToDate(Date fromDate, Date toDate) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = cb.createQuery(Member.class); Root<Member> member = criteria.from(Member.class); Path<Date> dateCreatedPath = member.get("createdDate"); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.lessThanOrEqualTo(dateCreatedPath, toDate)); predicates.add(cb.greaterThanOrEqualTo(dateCreatedPath, fromDate)); criteria.where(predicates.toArray(new Predicate[predicates.size()])); criteria.select(member).orderBy(cb.asc(member.get("name"))); return em.createQuery(criteria).getResultList(); }
From source file:net.groupbuy.dao.impl.PromotionDaoImpl.java
public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class); Root<Promotion> root = criteriaQuery.from(Promotion.class); criteriaQuery.select(root);/* w w w . jav a 2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date())); } } if (hasEnded != null) { if (hasEnded) { restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(), criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date()))); } } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Predicate generatePredicate(Root root, CriteriaBuilder builder, QueryParameter... queryParameters) { Predicate condition = builder.conjunction(); for (QueryParameter queryParameter : queryParameters) { Object value = queryParameter.getValue(); if (value == null || StringUtils.isBlank(value.toString())) { continue; }//from w w w.ja v a 2 s .c o m Path path = root.get(queryParameter.getKey()); switch (queryParameter.getMode()) { case BETWEEN: Object[] values = asArray(value); if (values != null) { condition = builder.and(builder.between((Path<Comparable>) path, asComparable(values[0]), asComparable(values[1]))); } break; case GREATER_THAN: condition = builder.and(condition, builder.greaterThan((Path<Comparable>) path, asComparable(value))); break; case GREATER_EQUALS: condition = builder.and(condition, builder.greaterThanOrEqualTo((Path<Comparable>) path, asComparable(value))); break; case LESS_THAN: condition = builder.and(condition, builder.lessThan((Path<Comparable>) path, asComparable(value))); break; case LESS_EQUALS: condition = builder.and(condition, builder.lessThanOrEqualTo((Path<Comparable>) path, asComparable(value))); break; case IS_NULL: condition = builder.and(condition, builder.isNull(path)); break; case IS_NOT_NULL: condition = builder.and(condition, builder.isNotNull(path)); break; case IN: condition = builder.and(condition, path.in(asArray(value))); break; case NOT_IN: condition = builder.and(condition, builder.not(path.in(asArray(value)))); break; case LIKE: condition = builder.and(condition, builder.like(path, "%" + String.valueOf(value) + "%")); break; case NOT_LIKE: condition = builder.and(condition, builder.notLike(path, "%" + String.valueOf(value) + "%")); break; case EQUALS: condition = builder.and(condition, builder.equal(path, value)); break; case NOT_EQUALS: condition = builder.and(condition, builder.notEqual(path, value)); break; default: break; } } return condition; }
From source file:com.wms.studio.service.WallpaperServiveImpl.java
@Override @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<WallpaperDto> findBy(final WallpaperEnum wallpaperType, final Date startDate, final Date endDate, PageSize pageSize) {//from ww w . jav a2 s . c o m if (pageSize == null) { pageSize = new PageSize(); } Page<Wallpaper> pageWallpaper = this.wallpaperRepository.findAll(new Specification<Wallpaper>() { @Override public Predicate toPredicate(Root<Wallpaper> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<Predicate>(); if (wallpaperType != null) { pres.add(cb.equal(root.get("wallpaperType").as(WallpaperEnum.class), wallpaperType)); } if (startDate != null) { pres.add(cb.greaterThanOrEqualTo(root.get("addDate").as(Date.class), startDate)); } if (endDate != null) { pres.add(cb.lessThanOrEqualTo(root.get("addDate").as(Date.class), endDate)); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit())); return this.wallpaperCovert.covertToDto(pageWallpaper); }
From source file:com.atschx.summer.core.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/*from w w w . j a va2 s . co m*/ 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]); } // 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; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
From source file:com.mxep.web.common.persistence.DynamicSpecifications.java
private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) { 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]); }//from www . j ava 2 s. co m /*????*/ if (expression.getJavaType().equals(Integer.class) && !filter.operator.equals(SearchFilter.Operator.IN)) { filter.value = Integer.parseInt(filter.value.toString()); } // 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 IN: In in = builder.in(expression); String value = filter.value.toString(); String[] values = StringUtils.split(value, ","); for (int i = 0; i < values.length; i++) { /*????*/ if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class) || expression.getJavaType().equals(byte.class)) { in.value(Integer.valueOf(values[i])); } else { in.value(values[i]); } } predicates.add(in); break; case NULL: predicates.add(builder.isNull(expression)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; } } return predicates; }