Refine search
@Transactional @Override public void deleteAll(Iterable<? extends T> entities) { Assert.notNull(entities, "The given Iterable of entities not be null!"); for (T entity : entities) { delete(entity); } }
@Transactional @Override public <S extends T> List<S> saveAll(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities not be null!"); List<S> result = new ArrayList<S>(); for (S entity : entities) { result.add(save(entity)); } return result; }
@Override protected <S extends E> Page<S> readPage(TypedQuery<S> query, Class<S> domainClass, Pageable pageable, @Nullable Specification<S> spec) { super.readPage(query, domainClass, pageable, spec); //首先查询满足条件的记录数 Long total = executeCountQuery(getCountQuery(spec, domainClass)); //检查查询的数据是否超出总记录数,如果超出总记录数,默认查询最后一页,并修改pageable参数 if (pageable.getOffset() >= total) { int pagenum = 0; //由于页号是从0开始,所以需要考虑整除的情况 if (total % pageable.getPageSize() == 0) { pagenum = Ints.checkedCast(Math.max(total / pageable.getPageSize() - 1, 0)); } else { pagenum = Ints.checkedCast(total / pageable.getPageSize()); } log.info("总记录数为 {} ,当前页号为 {} 。超出分页查询范围,只返回最后一页(第{}页)的数据。", total, pageable.getPageNumber(), pagenum); pageable = PageRequest.of(pagenum, pageable.getPageSize()); } //设置分页查询参数 query.setFirstResult(Long.valueOf(pageable.getOffset()).intValue()); query.setMaxResults(pageable.getPageSize()); List<S> content = total > pageable.getOffset() ? query.getResultList() : Collections.<S>emptyList(); return new PageImpl<S>(content, pageable, total); }
@Override public List<T> findAllById(Iterable<ID> ids) { Assert.notNull(ids, "The given Iterable of Id's must not be null!"); if (!ids.iterator().hasNext()) { return Collections.emptyList(); } if (entityInformation.hasCompositeId()) { List<T> results = new ArrayList<T>(); for (ID id : ids) { findById(id).ifPresent(results::add); } return results; } ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation); TypedQuery<T> query = getQuery(specification, Sort.unsorted()); return query.setParameter(specification.parameter, ids).getResultList(); }
@Override public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) { TypedQuery<T> query = getQuery(spec, pageable); return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList()) : readPage(query, getDomainClass(), pageable, spec); }
@Override public boolean existsById(ID id) { Assert.notNull(id, ID_MUST_NOT_BE_NULL); if (entityInformation.getIdAttribute() == null) { return findById(id).isPresent(); } String placeholder = provider.getCountQueryPlaceholder(); String entityName = entityInformation.getEntityName(); Iterable<String> idAttributeNames = entityInformation.getIdAttributeNames(); String existsQuery = QueryUtils.getExistsQueryString(entityName, placeholder, idAttributeNames); TypedQuery<Long> query = em.createQuery(existsQuery, Long.class); if (!entityInformation.hasCompositeId()) { query.setParameter(idAttributeNames.iterator().next(), id); return query.getSingleResult() == 1L; } for (String idAttributeName : idAttributeNames) { Object idAttributeValue = entityInformation.getCompositeIdAttributeValue(id, idAttributeName); boolean complexIdParameterValueDiscovered = idAttributeValue != null && !query.getParameter(idAttributeName).getParameterType().isAssignableFrom(idAttributeValue.getClass()); if (complexIdParameterValueDiscovered) { // fall-back to findById(id) which does the proper mapping for the parameter. return findById(id).isPresent(); } query.setParameter(idAttributeName, idAttributeValue); } return query.getSingleResult() == 1L; }
@Override public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) { ExampleSpecification<S> spec = new ExampleSpecification<>(example); Class<S> probeType = example.getProbeType(); TypedQuery<S> query = getQuery(new ExampleSpecification<>(example), probeType, pageable); return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); }
List<Object> resultSet = typedQuery.getResultList(); return (S) super.save(result); return super.save(entity); List<Object> resultSet = typedQuery.getResultList(); .toArray(String[]::new)); return (S) super.save(result); return super.save(entity);
/** * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and * {@link Specification}. * * @param query must not be {@literal null}. * @param spec can be {@literal null}. * @param pageable must not be {@literal null}. * @return * @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead */ @Deprecated protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Specification<T> spec) { return readPage(query, getDomainClass(), pageable, spec); }
@Override public Page<T> findAll(Specification<T> spec, Pageable pageable) { return delegate.findAll(spec, pageable); }
@Override public E getById(long id) { return super.findOne(id); }
@Override public long count() { return super.count((Specification<T>) null); }
@Override @Deprecated protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification<T> spec) { Page<T> page = super.readPage(query, pageable, spec); return pageable == null ? new WrappedPageImpl<T>(page.getContent()) : new WrappedPageImpl<T>(page.getContent(), pageable, page.getTotalElements()); }
/** * 基于主键查询单一数据对象 * * @param id * @return */ @Transactional(readOnly = true) public Optional<T> findOptionalOne(ID id) { return jpaRepository.findById(id); }
@Override public boolean existById(long id) { return super.exists(id); }
public List<T> findAllById(Iterable<ID> ids) { Assert.notNull(ids, "The given Iterable of Id's must not be null!"); if (!ids.iterator().hasNext()) { return Collections.emptyList(); } if (entityInformation.hasCompositeId()) { List<T> results = new ArrayList<T>(); for (ID id : ids) { findById(id).ifPresent(results::add); } return results; } ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation); TypedQuery<T> query = getQuery(specification, Sort.unsorted()); return query.setParameter(specification.parameter, ids).getResultList(); }