Skip to content

Commit 0a3fb22

Browse files
committed
Polishing.
Formatting. Fix warnings. See #2414 Original pull request #2419
1 parent 6018c7c commit 0a3fb22

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+16-28
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import jakarta.persistence.TypedQuery;
3535
import jakarta.persistence.criteria.CriteriaBuilder;
3636
import jakarta.persistence.criteria.CriteriaQuery;
37-
import jakarta.persistence.criteria.Order;
3837
import jakarta.persistence.criteria.ParameterExpression;
3938
import jakarta.persistence.criteria.Path;
4039
import jakarta.persistence.criteria.Predicate;
@@ -212,13 +211,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
212211
}
213212

214213
if (entityInformation.hasCompositeId()) {
215-
// XXX Hibernate just creates an empty Entity when doing the getById.
216-
// Others might do a select right away causing a big performance penalty.
217-
// See JavaDoc for getById.
214+
218215
List<T> entities = new ArrayList<>();
219-
ids.forEach(id -> entities.add(getById(id)));
216+
// generate entity (proxies) without accessing the database.
217+
ids.forEach(id -> entities.add(getReferenceById(id)));
220218
deleteAllInBatch(entities);
221219
} else {
220+
222221
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
223222
entityInformation.getIdAttribute().getName());
224223

@@ -292,7 +291,6 @@ public Optional<T> findById(ID id) {
292291
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
293292
* {@link EntityGraph} information.
294293
*
295-
* @return
296294
*/
297295
protected QueryHints getQueryHints() {
298296
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
@@ -377,7 +375,7 @@ public List<T> findAllById(Iterable<ID> ids) {
377375

378376
if (entityInformation.hasCompositeId()) {
379377

380-
List<T> results = new ArrayList<T>();
378+
List<T> results = new ArrayList<>();
381379

382380
for (ID id : ids) {
383381
findById(id).ifPresent(results::add);
@@ -388,7 +386,7 @@ public List<T> findAllById(Iterable<ID> ids) {
388386

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

391-
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
389+
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
392390
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
393391

394392
return query.setParameter(specification.parameter, idCollection).getResultList();
@@ -403,7 +401,7 @@ public List<T> findAll(Sort sort) {
403401
public Page<T> findAll(Pageable pageable) {
404402

405403
if (isUnpaged(pageable)) {
406-
return new PageImpl<T>(findAll());
404+
return new PageImpl<>(findAll());
407405
}
408406

409407
return findAll((Specification<T>) null, pageable);
@@ -428,7 +426,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
428426
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
429427

430428
TypedQuery<T> query = getQuery(spec, pageable);
431-
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
429+
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
432430
: readPage(query, getDomainClass(), pageable, spec);
433431
}
434432

@@ -442,7 +440,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
442440

443441
try {
444442
return Optional
445-
.of(getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
443+
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
446444
.getSingleResult());
447445
} catch (NoResultException e) {
448446
return Optional.empty();
@@ -452,7 +450,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
452450
@Override
453451
public <S extends T> long count(Example<S> example) {
454452
return executeCountQuery(
455-
getCountQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType()));
453+
getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType()));
456454
}
457455

458456
@Override
@@ -468,13 +466,13 @@ public <S extends T> boolean exists(Example<S> example) {
468466

469467
@Override
470468
public <S extends T> List<S> findAll(Example<S> example) {
471-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
469+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
472470
.getResultList();
473471
}
474472

475473
@Override
476474
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
477-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
475+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort)
478476
.getResultList();
479477
}
480478

@@ -548,7 +546,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
548546

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

551-
List<S> result = new ArrayList<S>();
549+
List<S> result = new ArrayList<>();
552550

553551
for (S entity : entities) {
554552
result.add(save(entity));
@@ -580,7 +578,6 @@ public void flush() {
580578
* @param query must not be {@literal null}.
581579
* @param spec can be {@literal null}.
582580
* @param pageable must not be {@literal null}.
583-
* @return
584581
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
585582
*/
586583
@Deprecated
@@ -596,7 +593,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
596593
* @param domainClass must not be {@literal null}.
597594
* @param spec can be {@literal null}.
598595
* @param pageable can be {@literal null}.
599-
* @return
600596
*/
601597
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
602598
@Nullable Specification<S> spec) {
@@ -615,7 +611,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
615611
*
616612
* @param spec can be {@literal null}.
617613
* @param pageable must not be {@literal null}.
618-
* @return
619614
*/
620615
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
621616

@@ -629,7 +624,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
629624
* @param spec can be {@literal null}.
630625
* @param domainClass must not be {@literal null}.
631626
* @param pageable must not be {@literal null}.
632-
* @return
633627
*/
634628
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
635629
Pageable pageable) {
@@ -643,7 +637,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
643637
*
644638
* @param spec can be {@literal null}.
645639
* @param sort must not be {@literal null}.
646-
* @return
647640
*/
648641
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
649642
return getQuery(spec, getDomainClass(), sort);
@@ -655,7 +648,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
655648
* @param spec can be {@literal null}.
656649
* @param domainClass must not be {@literal null}.
657650
* @param sort must not be {@literal null}.
658-
* @return
659651
*/
660652
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {
661653

@@ -676,7 +668,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
676668
* Creates a new count query for the given {@link Specification}.
677669
*
678670
* @param spec can be {@literal null}.
679-
* @return
680671
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
681672
*/
682673
@Deprecated
@@ -689,7 +680,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
689680
*
690681
* @param spec can be {@literal null}.
691682
* @param domainClass must not be {@literal null}.
692-
* @return
693683
*/
694684
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {
695685

@@ -705,7 +695,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
705695
}
706696

707697
// Remove all Orders the Specifications might have applied
708-
query.orderBy(Collections.<Order> emptyList());
698+
query.orderBy(Collections.emptyList());
709699

710700
return em.createQuery(query);
711701
}
@@ -716,7 +706,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
716706
* @param spec can be {@literal null}.
717707
* @param domainClass must not be {@literal null}.
718708
* @param query must not be {@literal null}.
719-
* @return
720709
*/
721710
private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specification<U> spec, Class<U> domainClass,
722711
CriteriaQuery<S> query) {
@@ -762,7 +751,6 @@ private void applyQueryHints(Query query) {
762751
* Executes a count query and transparently sums up all values returned.
763752
*
764753
* @param query must not be {@literal null}.
765-
* @return
766754
*/
767755
private static long executeCountQuery(TypedQuery<Long> query) {
768756

@@ -830,8 +818,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
830818
/**
831819
* Creates new {@link ExampleSpecification}.
832820
*
833-
* @param example
834-
* @param escapeCharacter
821+
* @param example the example to base the specification of. Must not be {@literal null}.
822+
* @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
835823
*/
836824
ExampleSpecification(Example<T> example, EscapeCharacter escapeCharacter) {
837825

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.List;
2223

2324
import jakarta.persistence.EntityManager;
@@ -113,15 +114,15 @@ void shouldSupportSavingEntitiesWithCompositeKeyClassesWithEmbeddedIdsAndDerived
113114
}
114115

115116
@Test // DATAJPA-472, DATAJPA-912
116-
void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
117+
void shouldSupportFindAllWithPageableAndEntityWithIdClass() {
117118

118119
IdClassExampleDepartment dep = new IdClassExampleDepartment();
119120
dep.setName("TestDepartment");
120121
dep.setDepartmentId(-1);
121122

122123
IdClassExampleEmployee emp = new IdClassExampleEmployee();
123124
emp.setDepartment(dep);
124-
emp = employeeRepositoryWithIdClass.save(emp);
125+
employeeRepositoryWithIdClass.save(emp);
125126

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

@@ -130,7 +131,7 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
130131
}
131132

132133
@Test // DATAJPA-2414
133-
void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {
134+
void shouldSupportDeleteAllByIdInBatchWithIdClass() {
134135

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

146-
employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key));
147+
employeeRepositoryWithIdClass.deleteAllByIdInBatch(Collections.singletonList(key));
147148

148149
em.flush();
149150
em.clear();
@@ -170,7 +171,7 @@ void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {
170171
EmbeddedIdExampleEmployee emp2 = new EmbeddedIdExampleEmployee();
171172
emp2.setEmployeePk(new EmbeddedIdExampleEmployeePK(2L, null));
172173
emp2.setDepartment(dep1);
173-
emp2 = employeeRepositoryWithEmbeddedId.save(emp2);
174+
employeeRepositoryWithEmbeddedId.save(emp2);
174175

175176
EmbeddedIdExampleEmployee emp3 = new EmbeddedIdExampleEmployee();
176177
emp3.setEmployeePk(new EmbeddedIdExampleEmployeePK(1L, null));
@@ -206,7 +207,7 @@ void sortByEmbeddedPkFieldInCompositePkWithIdClassInQueryDsl() {
206207
IdClassExampleEmployee emp2 = new IdClassExampleEmployee();
207208
emp2.setEmpId(2L);
208209
emp2.setDepartment(dep1);
209-
emp2 = employeeRepositoryWithIdClass.save(emp2);
210+
employeeRepositoryWithIdClass.save(emp2);
210211

211212
IdClassExampleEmployee emp3 = new IdClassExampleEmployee();
212213
emp3.setEmpId(1L);
@@ -276,7 +277,7 @@ void shouldAllowFindAllWithIdsForEntitiesWithCompoundIdClassKeys() {
276277
IdClassExampleEmployee emp1 = new IdClassExampleEmployee();
277278
emp1.setEmpId(3L);
278279
emp1.setDepartment(dep2);
279-
emp1 = employeeRepositoryWithIdClass.save(emp1);
280+
employeeRepositoryWithIdClass.save(emp1);
280281

281282
IdClassExampleDepartment dep1 = new IdClassExampleDepartment();
282283
dep1.setDepartmentId(1L);
@@ -285,7 +286,7 @@ void shouldAllowFindAllWithIdsForEntitiesWithCompoundIdClassKeys() {
285286
IdClassExampleEmployee emp2 = new IdClassExampleEmployee();
286287
emp2.setEmpId(2L);
287288
emp2.setDepartment(dep1);
288-
emp2 = employeeRepositoryWithIdClass.save(emp2);
289+
employeeRepositoryWithIdClass.save(emp2);
289290

290291
IdClassExampleEmployeePK emp1PK = new IdClassExampleEmployeePK();
291292
emp1PK.setDepartment(2L);

0 commit comments

Comments
 (0)