Skip to content

Commit fe71930

Browse files
committed
Polishing.
Reuse comment hint retrieval. Reorder methods. Inline methods and use fluent API where possible. See #2991 Original pull request: #2995
1 parent 943e66d commit fe71930

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

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

+47-38
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.Optional;
41+
import java.util.function.BiConsumer;
4142
import java.util.function.Function;
4243
import java.util.stream.Collectors;
4344
import java.util.stream.StreamSupport;
@@ -225,9 +226,11 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
225226
entityInformation.getIdAttribute().getName());
226227

227228
Query query = em.createQuery(queryString);
228-
/**
229+
230+
/*
229231
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
230232
*/
233+
231234
if (Collection.class.isInstance(ids)) {
232235
query.setParameter("ids", ids);
233236
} else {
@@ -299,33 +302,11 @@ public Optional<T> findById(ID id) {
299302
}
300303

301304
LockModeType type = metadata.getLockModeType();
302-
303-
Map<String, Object> hints = new HashMap<>();
304-
305-
getQueryHints().withFetchGraphs(em).forEach(hints::put);
306-
307-
if (metadata != null && metadata.getComment() != null && provider.getCommentHintKey() != null) {
308-
hints.put(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
309-
}
305+
Map<String, Object> hints = getHints();
310306

311307
return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints));
312308
}
313309

314-
/**
315-
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
316-
* {@link EntityGraph} information.
317-
*/
318-
protected QueryHints getQueryHints() {
319-
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
320-
}
321-
322-
/**
323-
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
324-
*/
325-
protected QueryHints getQueryHintsForCount() {
326-
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
327-
}
328-
329310
@Deprecated
330311
@Override
331312
public T getOne(ID id) {
@@ -432,7 +413,7 @@ public List<T> findAll(Sort sort) {
432413
@Override
433414
public Page<T> findAll(Pageable pageable) {
434415

435-
if (isUnpaged(pageable)) {
416+
if (pageable.isUnpaged()) {
436417
return new PageImpl<>(findAll());
437418
}
438419

@@ -458,7 +439,7 @@ public List<T> findAll(@Nullable Specification<T> spec) {
458439
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
459440

460441
TypedQuery<T> query = getQuery(spec, pageable);
461-
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
442+
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList())
462443
: readPage(query, getDomainClass(), pageable, spec);
463444
}
464445

@@ -489,9 +470,12 @@ public <S extends T> long count(Example<S> example) {
489470
public <S extends T> boolean exists(Example<S> example) {
490471

491472
Specification<S> spec = new ExampleSpecification<>(example, this.escapeCharacter);
492-
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class);
493-
cq.select(this.em.getCriteriaBuilder().literal(1));
473+
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
474+
.createQuery(Integer.class) //
475+
.select(this.em.getCriteriaBuilder().literal(1));
476+
494477
applySpecificationToCriteria(spec, example.getProbeType(), cq);
478+
495479
TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
496480
return query.setMaxResults(1).getResultList().size() == 1;
497481
}
@@ -541,7 +525,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
541525
Class<S> probeType = example.getProbeType();
542526
TypedQuery<S> query = getQuery(new ExampleSpecification<>(example, escapeCharacter), probeType, pageable);
543527

544-
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
528+
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
545529
}
546530

547531
@Override
@@ -777,6 +761,21 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
777761
return applyRepositoryMethodMetadataForCount(em.createQuery(query));
778762
}
779763

764+
/**
765+
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
766+
* {@link EntityGraph} information.
767+
*/
768+
protected QueryHints getQueryHints() {
769+
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
770+
}
771+
772+
/**
773+
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
774+
*/
775+
protected QueryHints getQueryHintsForCount() {
776+
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
777+
}
778+
780779
/**
781780
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
782781
*
@@ -827,10 +826,7 @@ private void applyQueryHints(Query query) {
827826
}
828827

829828
getQueryHints().withFetchGraphs(em).forEach(query::setHint);
830-
831-
if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
832-
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
833-
}
829+
applyComment(metadata, query::setHint);
834830
}
835831

836832
private <S> TypedQuery<S> applyRepositoryMethodMetadataForCount(TypedQuery<S> query) {
@@ -851,9 +847,26 @@ private void applyQueryHintsForCount(Query query) {
851847
}
852848

853849
getQueryHintsForCount().forEach(query::setHint);
850+
applyComment(metadata, query::setHint);
851+
}
852+
853+
private Map<String, Object> getHints() {
854+
855+
Map<String, Object> hints = new HashMap<>();
856+
857+
getQueryHints().withFetchGraphs(em).forEach(hints::put);
858+
859+
if (metadata != null) {
860+
applyComment(metadata, hints::put);
861+
}
862+
863+
return hints;
864+
}
865+
866+
private void applyComment(CrudMethodMetadata metadata, BiConsumer<String, Object> consumer) {
854867

855868
if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
856-
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
869+
consumer.accept(provider.getCommentHintKey(), provider.getCommentHintValue(this.metadata.getComment()));
857870
}
858871
}
859872

@@ -876,10 +889,6 @@ private static long executeCountQuery(TypedQuery<Long> query) {
876889
return total;
877890
}
878891

879-
private static boolean isUnpaged(Pageable pageable) {
880-
return pageable.isUnpaged();
881-
}
882-
883892
/**
884893
* Specification that gives access to the {@link Parameter} instance used to bind the ids for
885894
* {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses

0 commit comments

Comments
 (0)