Polishing.

Formatting.
Fix warnings.

See #2414
Original pull request #2419
This commit is contained in:
Jens Schauder
2022-02-08 14:50:00 +01:00
parent 6018c7c5f7
commit 0a3fb22860
2 changed files with 25 additions and 36 deletions

View File

@@ -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<T, ID> implements JpaRepositoryImplementation<T
}
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());
@@ -292,7 +291,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* 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);
@@ -377,7 +375,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
if (entityInformation.hasCompositeId()) {
List<T> results = new ArrayList<T>();
List<T> results = new ArrayList<>();
for (ID id : ids) {
findById(id).ifPresent(results::add);
@@ -388,7 +386,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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();
@@ -403,7 +401,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
public Page<T> findAll(Pageable pageable) {
if (isUnpaged(pageable)) {
return new PageImpl<T>(findAll());
return new PageImpl<>(findAll());
}
return findAll((Specification<T>) null, pageable);
@@ -428,7 +426,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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);
}
@@ -442,7 +440,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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();
@@ -452,7 +450,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
@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()));
}
@Override
@@ -468,13 +466,13 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
@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();
}
@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();
}
@@ -548,7 +546,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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));
@@ -580,7 +578,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* @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
@@ -596,7 +593,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* @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) {
@@ -615,7 +611,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
*
* @param spec can be {@literal null}.
* @param pageable must not be {@literal null}.
* @return
*/
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
@@ -629,7 +624,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* @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) {
@@ -643,7 +637,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
*
* @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);
@@ -655,7 +648,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* @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) {
@@ -676,7 +668,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* 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
@@ -689,7 +680,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
*
* @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) {
@@ -705,7 +695,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
}
// Remove all Orders the Specifications might have applied
query.orderBy(Collections.<Order> emptyList());
query.orderBy(Collections.emptyList());
return em.createQuery(query);
}
@@ -716,7 +706,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* @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) {
@@ -762,7 +751,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
* 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) {
@@ -830,8 +818,8 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<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) {

View File

@@ -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<IdClassExampleEmployee> 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);