Skip to content

Commit 072d7dd

Browse files
committed
Polishing.
Formatting. Fix warnings. See #2414 Original pull request #2419
1 parent b073d00 commit 072d7dd

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
223223
}
224224

225225
if (entityInformation.hasCompositeId()) {
226-
// XXX Hibernate just creates an empty Entity when doing the getById.
227-
// Others might do a select right away causing a big performance penalty.
228-
// See JavaDoc for getById.
226+
229227
List<T> entities = new ArrayList<>();
228+
// generate entity (proxies) without accessing the database.
230229
ids.forEach(id -> entities.add(getById(id)));
231230
deleteAllInBatch(entities);
232231
} else {
232+
233233
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
234234
entityInformation.getIdAttribute().getName());
235235

@@ -418,7 +418,7 @@ public List<T> findAllById(Iterable<ID> ids) {
418418

419419
if (entityInformation.hasCompositeId()) {
420420

421-
List<T> results = new ArrayList<T>();
421+
List<T> results = new ArrayList<>();
422422

423423
for (ID id : ids) {
424424
findById(id).ifPresent(results::add);
@@ -429,7 +429,7 @@ public List<T> findAllById(Iterable<ID> ids) {
429429

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

432-
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
432+
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
433433
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
434434

435435
return query.setParameter(specification.parameter, idCollection).getResultList();
@@ -452,7 +452,7 @@ public List<T> findAll(Sort sort) {
452452
public Page<T> findAll(Pageable pageable) {
453453

454454
if (isUnpaged(pageable)) {
455-
return new PageImpl<T>(findAll());
455+
return new PageImpl<>(findAll());
456456
}
457457

458458
return findAll((Specification<T>) null, pageable);
@@ -489,7 +489,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
489489
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
490490

491491
TypedQuery<T> query = getQuery(spec, pageable);
492-
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
492+
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
493493
: readPage(query, getDomainClass(), pageable, spec);
494494
}
495495

@@ -511,7 +511,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
511511

512512
try {
513513
return Optional
514-
.of(getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
514+
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
515515
.getSingleResult());
516516
} catch (NoResultException e) {
517517
return Optional.empty();
@@ -525,7 +525,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
525525
@Override
526526
public <S extends T> long count(Example<S> example) {
527527
return executeCountQuery(
528-
getCountQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType()));
528+
getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType()));
529529
}
530530

531531
/*
@@ -549,7 +549,7 @@ public <S extends T> boolean exists(Example<S> example) {
549549
*/
550550
@Override
551551
public <S extends T> List<S> findAll(Example<S> example) {
552-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
552+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
553553
.getResultList();
554554
}
555555

@@ -559,7 +559,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
559559
*/
560560
@Override
561561
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
562-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort).getResultList();
562+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort).getResultList();
563563
}
564564

565565
/*
@@ -636,7 +636,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
636636

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

639-
List<S> result = new ArrayList<S>();
639+
List<S> result = new ArrayList<>();
640640

641641
for (S entity : entities) {
642642
result.add(save(entity));
@@ -676,7 +676,6 @@ public void flush() {
676676
* @param query must not be {@literal null}.
677677
* @param spec can be {@literal null}.
678678
* @param pageable must not be {@literal null}.
679-
* @return
680679
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
681680
*/
682681
@Deprecated
@@ -692,7 +691,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
692691
* @param domainClass must not be {@literal null}.
693692
* @param spec can be {@literal null}.
694693
* @param pageable can be {@literal null}.
695-
* @return
696694
*/
697695
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
698696
@Nullable Specification<S> spec) {
@@ -711,7 +709,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
711709
*
712710
* @param spec can be {@literal null}.
713711
* @param pageable must not be {@literal null}.
714-
* @return
715712
*/
716713
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
717714

@@ -725,7 +722,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
725722
* @param spec can be {@literal null}.
726723
* @param domainClass must not be {@literal null}.
727724
* @param pageable must not be {@literal null}.
728-
* @return
729725
*/
730726
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
731727
Pageable pageable) {
@@ -739,7 +735,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
739735
*
740736
* @param spec can be {@literal null}.
741737
* @param sort must not be {@literal null}.
742-
* @return
743738
*/
744739
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
745740
return getQuery(spec, getDomainClass(), sort);
@@ -751,7 +746,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
751746
* @param spec can be {@literal null}.
752747
* @param domainClass must not be {@literal null}.
753748
* @param sort must not be {@literal null}.
754-
* @return
755749
*/
756750
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {
757751

@@ -772,7 +766,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
772766
* Creates a new count query for the given {@link Specification}.
773767
*
774768
* @param spec can be {@literal null}.
775-
* @return
776769
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
777770
*/
778771
@Deprecated
@@ -785,7 +778,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
785778
*
786779
* @param spec can be {@literal null}.
787780
* @param domainClass must not be {@literal null}.
788-
* @return
789781
*/
790782
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {
791783

@@ -801,7 +793,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
801793
}
802794

803795
// Remove all Orders the Specifications might have applied
804-
query.orderBy(Collections.<Order> emptyList());
796+
query.orderBy(Collections.emptyList());
805797

806798
return em.createQuery(query);
807799
}
@@ -812,7 +804,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
812804
* @param spec can be {@literal null}.
813805
* @param domainClass must not be {@literal null}.
814806
* @param query must not be {@literal null}.
815-
* @return
816807
*/
817808
private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specification<U> spec, Class<U> domainClass,
818809
CriteriaQuery<S> query) {
@@ -858,7 +849,6 @@ private void applyQueryHints(Query query) {
858849
* Executes a count query and transparently sums up all values returned.
859850
*
860851
* @param query must not be {@literal null}.
861-
* @return
862852
*/
863853
private static long executeCountQuery(TypedQuery<Long> query) {
864854

@@ -930,8 +920,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
930920
/**
931921
* Creates new {@link ExampleSpecification}.
932922
*
933-
* @param example
934-
* @param escapeCharacter
923+
* @param example the example to base the specification of. Must not be {@literal null}.
924+
* @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
935925
*/
936926
ExampleSpecification(Example<T> example, EscapeCharacter escapeCharacter) {
937927

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

Lines changed: 9 additions & 8 deletions
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 javax.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)