@Override public List<OrgTypeDO> findOrgTypesForScope(Long scopeId, Collection<String> orgTypeCodes) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<OrgTypeDO> criteria = cb.createQuery(OrgTypeDO.class); Root<OrgTypeDO> root = criteria.from(OrgTypeDO.class); criteria.where(cb.equal(root.get(OrgTypeDO_.scope).get(ScopeDO_.scopeId), scopeId)); In<String> inOrgTypeCode = cb.in(root.get(OrgTypeDO_.code)); if (orgTypeCodes.isEmpty()) { inOrgTypeCode.value((String) null); } else { for (String orgTypeCode : orgTypeCodes) { inOrgTypeCode.value(orgTypeCode); } } criteria.where(inOrgTypeCode); TypedQuery<OrgTypeDO> query = em.createQuery(criteria); query.setHint("org.hibernate.cacheable", Boolean.TRUE); return query.getResultList(); }
/** * 包含 * * @param propertyName 属性名称 * @param values 参数集合 * @return query实例 */ public Query whereIn(@NotNull final String propertyName, @NotNull final List<ParameterExpression> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.forEach(in::value); this.predicates.add(in); return this; }
/** * 包含 * * @param propertyName 属性名称 * @param values 参数值集合 * @return query实例 */ public Query whereValueIn(@NotNull final String propertyName, @NotNull final List<Object> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream() .map(this::makeParameter) .collect(Collectors.toList()) .forEach(in::value); this.predicates.add(in); return this; }
/** * 不包含 * * @param propertyName 属性名称 * @param values 参数集合 */ public Query whereNotIn(@NotNull final String propertyName, @NotNull final List<ParameterExpression> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.forEach(in::value); this.predicates.add(criteriaBuilder.not(in)); return this; }
/** * 不包含 * * @param propertyName 属性名称 * @param values 参数集合 */ public Query whereValueNotIn(@NotNull final String propertyName, @NotNull final List<Object> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream() .map(this::makeParameter) .collect(Collectors.toList()) .forEach(in::value); this.predicates.add(criteriaBuilder.not(in)); return this; }
/** * 包含 * * @param propertyName 属性名称 * @param values 参数集合 */ public Query whereIn(@NotNull final String propertyName, @NotNull final List<ParameterExpression> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream().forEach(in::value); this.predicates.add(in); return this; }
/** * 包含 * * @param propertyName 属性名称 * @param values 参数值集合 */ public Query whereValueIn(@NotNull final String propertyName, @NotNull final List<Object> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream() .map(this::makeParameter) .collect(Collectors.toList()) .stream() .forEach(in::value); this.predicates.add(in); return this; }
/** * 不包含 * * @param propertyName 属性名称 * @param values 参数集合 */ public Query whereNotIn(@NotNull final String propertyName, @NotNull final List<ParameterExpression> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream().forEach(in::value); this.predicates.add(criteriaBuilder.not(in)); return this; }
/** * 不包含 * * @param propertyName 属性名称 * @param values 参数集合 */ public Query whereValueNotIn(@NotNull final String propertyName, @NotNull final List<Object> values) { In in = criteriaBuilder.in(from.get(propertyName)); values.stream() .map(this::makeParameter) .collect(Collectors.toList()) .stream() .forEach(in::value); this.predicates.add(criteriaBuilder.not(in)); return this; }
protected <X> Specification<ENTITY> valueIn(SingularAttribute<? super ENTITY, X> field, final Collection<X> values) { return (root, query, builder) -> { In<X> in = builder.in(root.get(field)); for (X value : values) { in = in.value(value); } return in; }; }
protected <OTHER, X> Specification<ENTITY> valueIn(SingularAttribute<? super ENTITY, OTHER> reference, SingularAttribute<OTHER, X> valueField, final Collection<X> values) { return (root, query, builder) -> { In<X> in = builder.in(root.get(reference).get(valueField)); for (X value : values) { in = in.value(value); } return in; }; }
@Override public final List<T> findListByPropertyInList(final SingularAttribute<T, ? extends Object> property, final Object[] values) { final CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(getPersistentClass()); final Root<T> root = criteriaQuery.from(getPersistentClass()); criteriaQuery.select(root); final In<Object> in = criteriaBuilder.in((Path<Object>) root.get(property)); for (final Object object : values) { in.value(object); } criteriaQuery.where(in); final TypedQuery<T> typedQuery = getEntityManager().createQuery(criteriaQuery); addCacheHints(typedQuery, "findListByPropertyInList"); return typedQuery.getResultList(); }
@Override @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<ApplicationDto> findBy(final String userId, final String name, final Date startDate, final Date endDate, final ApplicationType typeGroup, final List<UserRole> roles, final Boolean auditingStatu, PageSize pageSize, SortDto sortDto) { if (pageSize == null) { pageSize = new PageSize(); } Page<Application> applications = applicationRepository.findAll( new Specification<Application>() { @Override public Predicate toPredicate(Root<Application> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<Predicate>(); if (!StringUtils.isBlank(userId)) { pres.add(cb.equal(root.get("addUser") .as(User.class), new User(userId))); } if (!StringUtils.isBlank(name)) { pres.add(cb.like(root.get("name").as(String.class), name)); } // 没办法。Mysql不支持bit查询 pres.add(cb.equal( root.get("auditingStatu").as(Integer.class), auditingStatu ? 1 : 0)); 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)); } if (typeGroup != null) { pres.add(cb.equal( root.get("typeGroup").as( ApplicationType.class), typeGroup)); } if (roles != null && !roles.isEmpty()) { In<UserRole> in = cb.in(root.get("role").as( UserRole.class)); for (UserRole role : roles) { in.value(role); } pres.add(in); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit(), SortUtils.covertSortDto(sortDto))); return applicationCovert.covertToDto(applications); }