Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Formatting.
Fix warnings.

See spring-projects#2414
Original pull request spring-projects#2419
  • Loading branch information
schauder committed Feb 8, 2022
1 parent 314889b commit d0c3b1a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,6 @@
*/
package org.springframework.data.jpa.repository.support;

import static org.springframework.data.jpa.repository.query.QueryUtils.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
Expand All @@ -62,6 +37,29 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import static org.springframework.data.jpa.repository.query.QueryUtils.*;

/**
* Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
* you a more sophisticated interface than the plain {@link EntityManager} .
Expand Down Expand Up @@ -228,13 +226,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
}

if (entityInformation.hasCompositeId()) {
// XXX Hibernate just creates an empty Entity when doing the getById.
// Others might do a select right away causing a big performance penalty.
// See JavaDoc for getById.

List<T> entities = new ArrayList<>();
ids.forEach(id -> entities.add(getById(id)));
// generate entity (proxies) without accessing the database.
ids.forEach(id -> entities.add(getReferenceById(id)));
deleteAllInBatch(entities);
} else {

String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
entityInformation.getIdAttribute().getName());

Expand Down Expand Up @@ -328,7 +326,6 @@ public Optional<T> findById(ID id) {
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
* {@link EntityGraph} information.
*
* @return
*/
protected QueryHints getQueryHints() {
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
Expand Down Expand Up @@ -433,7 +430,7 @@ public List<T> findAllById(Iterable<ID> ids) {

if (entityInformation.hasCompositeId()) {

List<T> results = new ArrayList<T>();
List<T> results = new ArrayList<>();

for (ID id : ids) {
findById(id).ifPresent(results::add);
Expand All @@ -444,7 +441,7 @@ public List<T> findAllById(Iterable<ID> ids) {

Collection<ID> idCollection = Streamable.of(ids).toList();

ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
TypedQuery<T> query = getQuery(specification, Sort.unsorted());

return query.setParameter(specification.parameter, idCollection).getResultList();
Expand All @@ -467,7 +464,7 @@ public List<T> findAll(Sort sort) {
public Page<T> findAll(Pageable pageable) {

if (isUnpaged(pageable)) {
return new PageImpl<T>(findAll());
return new PageImpl<>(findAll());
}

return findAll((Specification<T>) null, pageable);
Expand Down Expand Up @@ -504,7 +501,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {

TypedQuery<T> query = getQuery(spec, pageable);
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
: readPage(query, getDomainClass(), pageable, spec);
}

Expand All @@ -526,7 +523,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {

try {
return Optional
.of(getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
.getSingleResult());
} catch (NoResultException e) {
return Optional.empty();
Expand All @@ -540,7 +537,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
@Override
public <S extends T> long count(Example<S> example) {
return executeCountQuery(
getCountQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType()));
getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType()));
}

/*
Expand All @@ -564,7 +561,7 @@ public <S extends T> boolean exists(Example<S> example) {
*/
@Override
public <S extends T> List<S> findAll(Example<S> example) {
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
.getResultList();
}

Expand All @@ -574,7 +571,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
*/
@Override
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort)
.getResultList();
}

Expand Down Expand Up @@ -676,7 +673,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {

Assert.notNull(entities, "Entities must not be null!");

List<S> result = new ArrayList<S>();
List<S> result = new ArrayList<>();

for (S entity : entities) {
result.add(save(entity));
Expand Down Expand Up @@ -716,7 +713,6 @@ public void flush() {
* @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
Expand All @@ -732,7 +728,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
* @param domainClass must not be {@literal null}.
* @param spec can be {@literal null}.
* @param pageable can be {@literal null}.
* @return
*/
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
@Nullable Specification<S> spec) {
Expand All @@ -751,7 +746,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
*
* @param spec can be {@literal null}.
* @param pageable must not be {@literal null}.
* @return
*/
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {

Expand All @@ -765,7 +759,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
* @param spec can be {@literal null}.
* @param domainClass must not be {@literal null}.
* @param pageable must not be {@literal null}.
* @return
*/
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
Pageable pageable) {
Expand All @@ -779,7 +772,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
*
* @param spec can be {@literal null}.
* @param sort must not be {@literal null}.
* @return
*/
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
return getQuery(spec, getDomainClass(), sort);
Expand All @@ -791,7 +783,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
* @param spec can be {@literal null}.
* @param domainClass must not be {@literal null}.
* @param sort must not be {@literal null}.
* @return
*/
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {

Expand All @@ -812,7 +803,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
* Creates a new count query for the given {@link Specification}.
*
* @param spec can be {@literal null}.
* @return
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
*/
@Deprecated
Expand All @@ -825,7 +815,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
*
* @param spec can be {@literal null}.
* @param domainClass must not be {@literal null}.
* @return
*/
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {

Expand All @@ -841,7 +830,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
}

// Remove all Orders the Specifications might have applied
query.orderBy(Collections.<Order> emptyList());
query.orderBy(Collections.emptyList());

return em.createQuery(query);
}
Expand All @@ -852,7 +841,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
* @param spec can be {@literal null}.
* @param domainClass must not be {@literal null}.
* @param query must not be {@literal null}.
* @return
*/
private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specification<U> spec, Class<U> domainClass,
CriteriaQuery<S> query) {
Expand Down Expand Up @@ -898,7 +886,6 @@ private void applyQueryHints(Query query) {
* Executes a count query and transparently sums up all values returned.
*
* @param query must not be {@literal null}.
* @return
*/
private static long executeCountQuery(TypedQuery<Long> query) {

Expand Down Expand Up @@ -970,8 +957,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
/**
* Creates new {@link ExampleSpecification}.
*
* @param example
* @param escapeCharacter
* @param example the example to base the specification of. Must not be {@literal null}.
* @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
*/
ExampleSpecification(Example<T> example, EscapeCharacter escapeCharacter) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
Expand Down Expand Up @@ -113,15 +114,15 @@ void shouldSupportSavingEntitiesWithCompositeKeyClassesWithEmbeddedIdsAndDerived
}

@Test // DATAJPA-472, DATAJPA-912
void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
void shouldSupportFindAllWithPageableAndEntityWithIdClass() {

IdClassExampleDepartment dep = new IdClassExampleDepartment();
dep.setName("TestDepartment");
dep.setDepartmentId(-1);

IdClassExampleEmployee emp = new IdClassExampleEmployee();
emp.setDepartment(dep);
emp = employeeRepositoryWithIdClass.save(emp);
employeeRepositoryWithIdClass.save(emp);

Page<IdClassExampleEmployee> page = employeeRepositoryWithIdClass.findAll(PageRequest.of(0, 1));

Expand All @@ -130,7 +131,7 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
}

@Test // DATAJPA-2414
void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {
void shouldSupportDeleteAllByIdInBatchWithIdClass() {

IdClassExampleDepartment dep = new IdClassExampleDepartment();
dep.setName("TestDepartment");
Expand All @@ -143,7 +144,7 @@ void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {
IdClassExampleEmployeePK key = new IdClassExampleEmployeePK(emp.getEmpId(), dep.getDepartmentId());
assertThat(employeeRepositoryWithIdClass.findById(key)).isNotEmpty();

employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key));
employeeRepositoryWithIdClass.deleteAllByIdInBatch(Collections.singletonList(key));

em.flush();
em.clear();
Expand All @@ -170,7 +171,7 @@ void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {
EmbeddedIdExampleEmployee emp2 = new EmbeddedIdExampleEmployee();
emp2.setEmployeePk(new EmbeddedIdExampleEmployeePK(2L, null));
emp2.setDepartment(dep1);
emp2 = employeeRepositoryWithEmbeddedId.save(emp2);
employeeRepositoryWithEmbeddedId.save(emp2);

EmbeddedIdExampleEmployee emp3 = new EmbeddedIdExampleEmployee();
emp3.setEmployeePk(new EmbeddedIdExampleEmployeePK(1L, null));
Expand Down Expand Up @@ -206,7 +207,7 @@ void sortByEmbeddedPkFieldInCompositePkWithIdClassInQueryDsl() {
IdClassExampleEmployee emp2 = new IdClassExampleEmployee();
emp2.setEmpId(2L);
emp2.setDepartment(dep1);
emp2 = employeeRepositoryWithIdClass.save(emp2);
employeeRepositoryWithIdClass.save(emp2);

IdClassExampleEmployee emp3 = new IdClassExampleEmployee();
emp3.setEmpId(1L);
Expand Down Expand Up @@ -276,7 +277,7 @@ void shouldAllowFindAllWithIdsForEntitiesWithCompoundIdClassKeys() {
IdClassExampleEmployee emp1 = new IdClassExampleEmployee();
emp1.setEmpId(3L);
emp1.setDepartment(dep2);
emp1 = employeeRepositoryWithIdClass.save(emp1);
employeeRepositoryWithIdClass.save(emp1);

IdClassExampleDepartment dep1 = new IdClassExampleDepartment();
dep1.setDepartmentId(1L);
Expand All @@ -285,7 +286,7 @@ void shouldAllowFindAllWithIdsForEntitiesWithCompoundIdClassKeys() {
IdClassExampleEmployee emp2 = new IdClassExampleEmployee();
emp2.setEmpId(2L);
emp2.setDepartment(dep1);
emp2 = employeeRepositoryWithIdClass.save(emp2);
employeeRepositoryWithIdClass.save(emp2);

IdClassExampleEmployeePK emp1PK = new IdClassExampleEmployeePK();
emp1PK.setDepartment(2L);
Expand Down

0 comments on commit d0c3b1a

Please sign in to comment.