From 0a3fb228604cf02239a39d51ad42919cd65730dd Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 8 Feb 2022 14:50:00 +0100 Subject: [PATCH] Polishing. Formatting. Fix warnings. See #2414 Original pull request #2419 --- .../support/SimpleJpaRepository.java | 44 +++++++------------ .../RepositoryWithCompositeKeyTests.java | 17 +++---- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index e36b09452..3c1639f04 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -34,7 +34,6 @@ import jakarta.persistence.Query; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.Order; import jakarta.persistence.criteria.ParameterExpression; import jakarta.persistence.criteria.Path; import jakarta.persistence.criteria.Predicate; @@ -212,13 +211,13 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation 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()); @@ -292,7 +291,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation results = new ArrayList(); + List results = new ArrayList<>(); for (ID id : ids) { findById(id).ifPresent(results::add); @@ -388,7 +386,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation idCollection = Streamable.of(ids).toList(); - ByIdsSpecification specification = new ByIdsSpecification(entityInformation); + ByIdsSpecification specification = new ByIdsSpecification<>(entityInformation); TypedQuery query = getQuery(specification, Sort.unsorted()); return query.setParameter(specification.parameter, idCollection).getResultList(); @@ -403,7 +401,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation findAll(Pageable pageable) { if (isUnpaged(pageable)) { - return new PageImpl(findAll()); + return new PageImpl<>(findAll()); } return findAll((Specification) null, pageable); @@ -428,7 +426,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation findAll(@Nullable Specification spec, Pageable pageable) { TypedQuery query = getQuery(spec, pageable); - return isUnpaged(pageable) ? new PageImpl(query.getResultList()) + return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, getDomainClass(), pageable, spec); } @@ -442,7 +440,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) + .of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) .getSingleResult()); } catch (NoResultException e) { return Optional.empty(); @@ -452,7 +450,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation long count(Example example) { return executeCountQuery( - getCountQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType())); + getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType())); } @Override @@ -468,13 +466,13 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation List findAll(Example example) { - return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) + return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) .getResultList(); } @Override public List findAll(Example example, Sort sort) { - return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), sort) + return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort) .getResultList(); } @@ -548,7 +546,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation result = new ArrayList(); + List result = new ArrayList<>(); for (S entity : entities) { result.add(save(entity)); @@ -580,7 +578,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation Page readPage(TypedQuery query, final Class domainClass, Pageable pageable, @Nullable Specification spec) { @@ -615,7 +611,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation getQuery(@Nullable Specification spec, Pageable pageable) { @@ -629,7 +624,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation TypedQuery getQuery(@Nullable Specification spec, Class domainClass, Pageable pageable) { @@ -643,7 +637,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation getQuery(@Nullable Specification spec, Sort sort) { return getQuery(spec, getDomainClass(), sort); @@ -655,7 +648,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation TypedQuery getQuery(@Nullable Specification spec, Class domainClass, Sort sort) { @@ -676,7 +668,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation TypedQuery getCountQuery(@Nullable Specification spec, Class domainClass) { @@ -705,7 +695,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation emptyList()); + query.orderBy(Collections.emptyList()); return em.createQuery(query); } @@ -716,7 +706,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation Root applySpecificationToCriteria(@Nullable Specification spec, Class domainClass, CriteriaQuery query) { @@ -762,7 +751,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation query) { @@ -830,8 +818,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation example, EscapeCharacter escapeCharacter) { diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java index 150f9486c..18f1574af 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java @@ -18,6 +18,7 @@ package org.springframework.data.jpa.repository; import static org.assertj.core.api.Assertions.*; import java.util.Arrays; +import java.util.Collections; import java.util.List; import jakarta.persistence.EntityManager; @@ -113,7 +114,7 @@ public class RepositoryWithCompositeKeyTests { } @Test // DATAJPA-472, DATAJPA-912 - void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception { + void shouldSupportFindAllWithPageableAndEntityWithIdClass() { IdClassExampleDepartment dep = new IdClassExampleDepartment(); dep.setName("TestDepartment"); @@ -121,7 +122,7 @@ public class RepositoryWithCompositeKeyTests { IdClassExampleEmployee emp = new IdClassExampleEmployee(); emp.setDepartment(dep); - emp = employeeRepositoryWithIdClass.save(emp); + employeeRepositoryWithIdClass.save(emp); Page page = employeeRepositoryWithIdClass.findAll(PageRequest.of(0, 1)); @@ -130,7 +131,7 @@ public class RepositoryWithCompositeKeyTests { } @Test // DATAJPA-2414 - void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception { + void shouldSupportDeleteAllByIdInBatchWithIdClass() { IdClassExampleDepartment dep = new IdClassExampleDepartment(); dep.setName("TestDepartment"); @@ -143,7 +144,7 @@ public class RepositoryWithCompositeKeyTests { 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(); @@ -170,7 +171,7 @@ public class RepositoryWithCompositeKeyTests { 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)); @@ -206,7 +207,7 @@ public class RepositoryWithCompositeKeyTests { IdClassExampleEmployee emp2 = new IdClassExampleEmployee(); emp2.setEmpId(2L); emp2.setDepartment(dep1); - emp2 = employeeRepositoryWithIdClass.save(emp2); + employeeRepositoryWithIdClass.save(emp2); IdClassExampleEmployee emp3 = new IdClassExampleEmployee(); emp3.setEmpId(1L); @@ -276,7 +277,7 @@ public class RepositoryWithCompositeKeyTests { IdClassExampleEmployee emp1 = new IdClassExampleEmployee(); emp1.setEmpId(3L); emp1.setDepartment(dep2); - emp1 = employeeRepositoryWithIdClass.save(emp1); + employeeRepositoryWithIdClass.save(emp1); IdClassExampleDepartment dep1 = new IdClassExampleDepartment(); dep1.setDepartmentId(1L); @@ -285,7 +286,7 @@ public class RepositoryWithCompositeKeyTests { IdClassExampleEmployee emp2 = new IdClassExampleEmployee(); emp2.setEmpId(2L); emp2.setDepartment(dep1); - emp2 = employeeRepositoryWithIdClass.save(emp2); + employeeRepositoryWithIdClass.save(emp2); IdClassExampleEmployeePK emp1PK = new IdClassExampleEmployeePK(); emp1PK.setDepartment(2L);