diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 10b5c5730..adbeb03c2 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -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; @@ -62,6 +37,29 @@ import org.springframework.stereotype.Repository; 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} . @@ -228,13 +226,13 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation entities = new ArrayList<>(); + // generate entity (proxies) without accessing the database. ids.forEach(id -> entities.add(getById(id))); deleteAllInBatch(entities); } else { + String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), entityInformation.getIdAttribute().getName()); @@ -328,7 +326,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation results = new ArrayList(); + List results = new ArrayList<>(); for (ID id : ids) { findById(id).ifPresent(results::add); @@ -436,7 +433,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(); @@ -459,7 +456,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); @@ -496,7 +493,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); } @@ -518,7 +515,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(); @@ -532,7 +529,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())); } /* @@ -556,7 +553,7 @@ 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(); } @@ -566,7 +563,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation 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(); } @@ -668,7 +665,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation result = new ArrayList(); + List result = new ArrayList<>(); for (S entity : entities) { result.add(save(entity)); @@ -708,7 +705,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation Page readPage(TypedQuery query, final Class domainClass, Pageable pageable, @Nullable Specification spec) { @@ -743,7 +738,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation getQuery(@Nullable Specification spec, Pageable pageable) { @@ -757,7 +751,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation TypedQuery getQuery(@Nullable Specification spec, Class domainClass, Pageable pageable) { @@ -771,7 +764,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation getQuery(@Nullable Specification spec, Sort sort) { return getQuery(spec, getDomainClass(), sort); @@ -783,7 +775,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation TypedQuery getQuery(@Nullable Specification spec, Class domainClass, Sort sort) { @@ -804,7 +795,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation TypedQuery getCountQuery(@Nullable Specification spec, Class domainClass) { @@ -833,7 +822,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation emptyList()); + query.orderBy(Collections.emptyList()); return em.createQuery(query); } @@ -844,7 +833,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation Root applySpecificationToCriteria(@Nullable Specification spec, Class domainClass, CriteriaQuery query) { @@ -890,7 +878,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation query) { @@ -962,8 +949,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation example, EscapeCharacter escapeCharacter) { diff --git a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java b/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java index c6260fac9..ac4c22938 100644 --- a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java +++ b/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 javax.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);