Skip to content

Commit a5c5786

Browse files
committed
Polishing.
Formatting. Fix warnings. See #2414 Original pull request #2419
1 parent e85ad50 commit a5c5786

File tree

2 files changed

+47
-59
lines changed

2 files changed

+47
-59
lines changed

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

+38-51
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<>();
231+
// generate entity (proxies) without accessing the database.
235232
ids.forEach(id -> entities.add(getById(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);
@@ -425,7 +422,7 @@ public List<T> findAllById(Iterable<ID> ids) {
425422

426423
if (entityInformation.hasCompositeId()) {
427424

428-
List<T> results = new ArrayList<T>();
425+
List<T> results = new ArrayList<>();
429426

430427
for (ID id : ids) {
431428
findById(id).ifPresent(results::add);
@@ -436,7 +433,7 @@ public List<T> findAllById(Iterable<ID> ids) {
436433

437434
Collection<ID> idCollection = Streamable.of(ids).toList();
438435

439-
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
436+
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
440437
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
441438

442439
return query.setParameter(specification.parameter, idCollection).getResultList();
@@ -459,7 +456,7 @@ public List<T> findAll(Sort sort) {
459456
public Page<T> findAll(Pageable pageable) {
460457

461458
if (isUnpaged(pageable)) {
462-
return new PageImpl<T>(findAll());
459+
return new PageImpl<>(findAll());
463460
}
464461

465462
return findAll((Specification<T>) null, pageable);
@@ -496,7 +493,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
496493
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
497494

498495
TypedQuery<T> query = getQuery(spec, pageable);
499-
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
496+
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
500497
: readPage(query, getDomainClass(), pageable, spec);
501498
}
502499

@@ -518,7 +515,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
518515

519516
try {
520517
return Optional
521-
.of(getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
518+
.of(getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
522519
.getSingleResult());
523520
} catch (NoResultException e) {
524521
return Optional.empty();
@@ -532,7 +529,7 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
532529
@Override
533530
public <S extends T> long count(Example<S> example) {
534531
return executeCountQuery(
535-
getCountQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType()));
532+
getCountQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType()));
536533
}
537534

538535
/*
@@ -556,7 +553,7 @@ public <S extends T> boolean exists(Example<S> example) {
556553
*/
557554
@Override
558555
public <S extends T> List<S> findAll(Example<S> example) {
559-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
556+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), Sort.unsorted())
560557
.getResultList();
561558
}
562559

@@ -566,7 +563,7 @@ public <S extends T> List<S> findAll(Example<S> example) {
566563
*/
567564
@Override
568565
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
569-
return getQuery(new ExampleSpecification<S>(example, escapeCharacter), example.getProbeType(), sort)
566+
return getQuery(new ExampleSpecification<>(example, escapeCharacter), example.getProbeType(), sort)
570567
.getResultList();
571568
}
572569

@@ -668,7 +665,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
668665

669666
Assert.notNull(entities, "Entities must not be null!");
670667

671-
List<S> result = new ArrayList<S>();
668+
List<S> result = new ArrayList<>();
672669

673670
for (S entity : entities) {
674671
result.add(save(entity));
@@ -708,7 +705,6 @@ public void flush() {
708705
* @param query must not be {@literal null}.
709706
* @param spec can be {@literal null}.
710707
* @param pageable must not be {@literal null}.
711-
* @return
712708
* @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead
713709
*/
714710
@Deprecated
@@ -724,7 +720,6 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Spe
724720
* @param domainClass must not be {@literal null}.
725721
* @param spec can be {@literal null}.
726722
* @param pageable can be {@literal null}.
727-
* @return
728723
*/
729724
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
730725
@Nullable Specification<S> spec) {
@@ -743,7 +738,6 @@ protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> dom
743738
*
744739
* @param spec can be {@literal null}.
745740
* @param pageable must not be {@literal null}.
746-
* @return
747741
*/
748742
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pageable) {
749743

@@ -757,7 +751,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Pageable pagea
757751
* @param spec can be {@literal null}.
758752
* @param domainClass must not be {@literal null}.
759753
* @param pageable must not be {@literal null}.
760-
* @return
761754
*/
762755
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass,
763756
Pageable pageable) {
@@ -771,7 +764,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
771764
*
772765
* @param spec can be {@literal null}.
773766
* @param sort must not be {@literal null}.
774-
* @return
775767
*/
776768
protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
777769
return getQuery(spec, getDomainClass(), sort);
@@ -783,7 +775,6 @@ protected TypedQuery<T> getQuery(@Nullable Specification<T> spec, Sort sort) {
783775
* @param spec can be {@literal null}.
784776
* @param domainClass must not be {@literal null}.
785777
* @param sort must not be {@literal null}.
786-
* @return
787778
*/
788779
protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, Sort sort) {
789780

@@ -804,7 +795,6 @@ protected <S extends T> TypedQuery<S> getQuery(@Nullable Specification<S> spec,
804795
* Creates a new count query for the given {@link Specification}.
805796
*
806797
* @param spec can be {@literal null}.
807-
* @return
808798
* @deprecated override {@link #getCountQuery(Specification, Class)} instead
809799
*/
810800
@Deprecated
@@ -817,7 +807,6 @@ protected TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec) {
817807
*
818808
* @param spec can be {@literal null}.
819809
* @param domainClass must not be {@literal null}.
820-
* @return
821810
*/
822811
protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass) {
823812

@@ -833,7 +822,7 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
833822
}
834823

835824
// Remove all Orders the Specifications might have applied
836-
query.orderBy(Collections.<Order> emptyList());
825+
query.orderBy(Collections.emptyList());
837826

838827
return em.createQuery(query);
839828
}
@@ -844,7 +833,6 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
844833
* @param spec can be {@literal null}.
845834
* @param domainClass must not be {@literal null}.
846835
* @param query must not be {@literal null}.
847-
* @return
848836
*/
849837
private <S, U extends T> Root<U> applySpecificationToCriteria(@Nullable Specification<U> spec, Class<U> domainClass,
850838
CriteriaQuery<S> query) {
@@ -890,7 +878,6 @@ private void applyQueryHints(Query query) {
890878
* Executes a count query and transparently sums up all values returned.
891879
*
892880
* @param query must not be {@literal null}.
893-
* @return
894881
*/
895882
private static long executeCountQuery(TypedQuery<Long> query) {
896883

@@ -962,8 +949,8 @@ private static class ExampleSpecification<T> implements Specification<T> {
962949
/**
963950
* Creates new {@link ExampleSpecification}.
964951
*
965-
* @param example
966-
* @param escapeCharacter
952+
* @param example the example to base the specification of. Must not be {@literal null}.
953+
* @param escapeCharacter the escape character to use for like expressions. Must not be {@literal null}.
967954
*/
968955
ExampleSpecification(Example<T> example, EscapeCharacter escapeCharacter) {
969956

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)