Skip to content

Commit d0c3b1a

Browse files
committed
Polishing.
Formatting. Fix warnings. See #2414 Original pull request #2419
1 parent 314889b commit d0c3b1a

File tree

2 files changed

+48
-60
lines changed

2 files changed

+48
-60
lines changed

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

+39-52
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,6 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18-
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
19-
20-
import java.util.ArrayList;
21-
import java.util.Collection;
22-
import java.util.Collections;
23-
import java.util.HashMap;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Optional;
27-
import java.util.function.Function;
28-
29-
import javax.persistence.EntityManager;
30-
import javax.persistence.LockModeType;
31-
import javax.persistence.NoResultException;
32-
import javax.persistence.Parameter;
33-
import javax.persistence.Query;
34-
import javax.persistence.TypedQuery;
35-
import javax.persistence.criteria.CriteriaBuilder;
36-
import javax.persistence.criteria.CriteriaQuery;
37-
import javax.persistence.criteria.Order;
38-
import javax.persistence.criteria.ParameterExpression;
39-
import javax.persistence.criteria.Path;
40-
import javax.persistence.criteria.Predicate;
41-
import javax.persistence.criteria.Root;
42-
4318
import org.springframework.dao.EmptyResultDataAccessException;
4419
import org.springframework.data.domain.Example;
4520
import org.springframework.data.domain.Page;
@@ -62,6 +37,29 @@
6237
import org.springframework.transaction.annotation.Transactional;
6338
import org.springframework.util.Assert;
6439

40+
import javax.persistence.EntityManager;
41+
import javax.persistence.LockModeType;
42+
import javax.persistence.NoResultException;
43+
import javax.persistence.Parameter;
44+
import javax.persistence.Query;
45+
import javax.persistence.TypedQuery;
46+
import javax.persistence.criteria.CriteriaBuilder;
47+
import javax.persistence.criteria.CriteriaQuery;
48+
import javax.persistence.criteria.ParameterExpression;
49+
import javax.persistence.criteria.Path;
50+
import javax.persistence.criteria.Predicate;
51+
import javax.persistence.criteria.Root;
52+
import java.util.ArrayList;
53+
import java.util.Collection;
54+
import java.util.Collections;
55+
import java.util.HashMap;
56+
import java.util.List;
57+
import java.util.Map;
58+
import java.util.Optional;
59+
import java.util.function.Function;
60+
61+
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
62+
6563
/**
6664
* Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
6765
* you a more sophisticated interface than the plain {@link EntityManager} .
@@ -228,13 +226,13 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
228226
}
229227

230228
if (entityInformation.hasCompositeId()) {
231-
// XXX Hibernate just creates an empty Entity when doing the getById.
232-
// Others might do a select right away causing a big performance penalty.
233-
// See JavaDoc for getById.
229+
234230
List<T> entities = new ArrayList<>();
235-
ids.forEach(id -> entities.add(getById(id)));
231+
// generate entity (proxies) without accessing the database.
232+
ids.forEach(id -> entities.add(getReferenceById(id)));
236233
deleteAllInBatch(entities);
237234
} else {
235+
238236
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
239237
entityInformation.getIdAttribute().getName());
240238

@@ -328,7 +326,6 @@ public Optional<T> findById(ID id) {
328326
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
329327
* {@link EntityGraph} information.
330328
*
331-
* @return
332329
*/
333330
protected QueryHints getQueryHints() {
334331
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
@@ -433,7 +430,7 @@ public List<T> findAllById(Iterable<ID> ids) {
433430

434431
if (entityInformation.hasCompositeId()) {
435432

436-
List<T> results = new ArrayList<T>();
433+
List<T> results = new ArrayList<>();
437434

438435
for (ID id : ids) {
439436
findById(id).ifPresent(results::add);
@@ -444,7 +441,7 @@ public List<T> findAllById(Iterable<ID> ids) {
444441

445442
Collection<ID> idCollection = Streamable.of(ids).toList();
446443

447-
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
444+
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
448445
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
449446

450447
return query.setParameter(specification.parameter, idCollection).getResultList();
@@ -467,7 +464,7 @@ public List<T> findAll(Sort sort) {
467464
public Page<T> findAll(Pageable pageable) {
468465

469466
if (isUnpaged(pageable)) {
470-
return new PageImpl<T>(findAll());
467+
return new PageImpl<>(findAll());
471468
}
472469

473470
return findAll((Specification<T>) null, pageable);
@@ -504,7 +501,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
504501
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
505502

506503
TypedQuery<T> query = getQuery(spec, pageable);
507-
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
504+
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
508505
: readPage(query, getDomainClass(), pageable, spec);
509506
}
510507

@@ -526,7 +523,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
526523

527524
try {
528525
return Optional
529-
.of(getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
526+
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
530527
.getSingleResult());
531528
} catch (NoResultException e) {
532529
return Optional.empty();
@@ -540,7 +537,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
540537
@Override
541538
public <S extends T> long count(Example<S> example) {
542539
return executeCountQuery(
543-
getCountQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType()));
540+
getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType()));
544541
}
545542

546543
/*
@@ -564,7 +561,7 @@ public <S extends T> boolean exists(Example<S> example) {
564561
*/
565562
@Override
566563
public <S extends T> List<S> findAll(Example<S> example) {
567-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
564+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
568565
.getResultList();
569566
}
570567

@@ -574,7 +571,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
574571
*/
575572
@Override
576573
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
577-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
574+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort)
578575
.getResultList();
579576
}
580577

@@ -676,7 +673,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
676673

677674
Assert.notNull(entities, "Entities must not be null!");
678675

679-
List<S> result = new ArrayList<S>();
676+
List<S> result = new ArrayList<>();
680677

681678
for (S entity : entities) {
682679
result.add(save(entity));
@@ -716,7 +713,6 @@ public void flush() {
716713
* @param query must not be {@literal null}.
717714
* @param spec can be {@literal null}.
718715
* @param pageable must not be {@literal null}.
719-
* @return
720716
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
721717
*/
722718
@Deprecated
@@ -732,7 +728,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
732728
* @param domainClass must not be {@literal null}.
733729
* @param spec can be {@literal null}.
734730
* @param pageable can be {@literal null}.
735-
* @return
736731
*/
737732
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
738733
@Nullable Specification<S> spec) {
@@ -751,7 +746,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
751746
*
752747
* @param spec can be {@literal null}.
753748
* @param pageable must not be {@literal null}.
754-
* @return
755749
*/
756750
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
757751

@@ -765,7 +759,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
765759
* @param spec can be {@literal null}.
766760
* @param domainClass must not be {@literal null}.
767761
* @param pageable must not be {@literal null}.
768-
* @return
769762
*/
770763
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
771764
Pageable pageable) {
@@ -779,7 +772,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
779772
*
780773
* @param spec can be {@literal null}.
781774
* @param sort must not be {@literal null}.
782-
* @return
783775
*/
784776
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
785777
return getQuery(spec, getDomainClass(), sort);
@@ -791,7 +783,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
791783
* @param spec can be {@literal null}.
792784
* @param domainClass must not be {@literal null}.
793785
* @param sort must not be {@literal null}.
794-
* @return
795786
*/
796787
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {
797788

@@ -812,7 +803,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
812803
* Creates a new count query for the given {@link Specification}.
813804
*
814805
* @param spec can be {@literal null}.
815-
* @return
816806
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
817807
*/
818808
@Deprecated
@@ -825,7 +815,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
825815
*
826816
* @param spec can be {@literal null}.
827817
* @param domainClass must not be {@literal null}.
828-
* @return
829818
*/
830819
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {
831820

@@ -841,7 +830,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
841830
}
842831

843832
// Remove all Orders the Specifications might have applied
844-
query.orderBy(Collections.<Order> emptyList());
833+
query.orderBy(Collections.emptyList());
845834

846835
return em.createQuery(query);
847836
}
@@ -852,7 +841,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
852841
* @param spec can be {@literal null}.
853842
* @param domainClass must not be {@literal null}.
854843
* @param query must not be {@literal null}.
855-
* @return
856844
*/
857845
private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specification<U> spec, Class<U> domainClass,
858846
CriteriaQuery<S> query) {
@@ -898,7 +886,6 @@ private void applyQueryHints(Query query) {
898886
* Executes a count query and transparently sums up all values returned.
899887
*
900888
* @param query must not be {@literal null}.
901-
* @return
902889
*/
903890
private static long executeCountQuery(TypedQuery<Long> query) {
904891

@@ -970,8 +957,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
970957
/**
971958
* Creates new {@link ExampleSpecification}.
972959
*
973-
* @param example
974-
* @param escapeCharacter
960+
* @param example the example to base the specification of. Must not be {@literal null}.
961+
* @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
975962
*/
976963
ExampleSpecification(Example<T> example, EscapeCharacter escapeCharacter) {
977964

Diff for: 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 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)