Skip to content

Commit 2df06bd

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 168d7d5 commit 2df06bd

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

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

+52-40
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;
@@ -230,9 +231,11 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
230231
entityInformation.getIdAttribute().getName());
231232

232233
Query query = em.createQuery(queryString);
233-
/**
234+
235+
/*
234236
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
235237
*/
238+
236239
if (Collection.class.isInstance(ids)) {
237240
query.setParameter("ids", ids);
238241
} else {
@@ -304,33 +307,11 @@ public Optional<T> findById(ID id) {
304307
}
305308

306309
LockModeType type = metadata.getLockModeType();
307-
308-
Map<String, Object> hints = new HashMap<>();
309-
310-
getQueryHints().withFetchGraphs(em).forEach(hints::put);
311-
312-
if (metadata != null && metadata.getComment() != null && provider.getCommentHintKey() != null) {
313-
hints.put(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
314-
}
310+
Map<String, Object> hints = getHints();
315311

316312
return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints));
317313
}
318314

319-
/**
320-
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
321-
* {@link EntityGraph} information.
322-
*/
323-
protected QueryHints getQueryHints() {
324-
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
325-
}
326-
327-
/**
328-
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
329-
*/
330-
protected QueryHints getQueryHintsForCount() {
331-
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
332-
}
333-
334315
@Deprecated
335316
@Override
336317
public T getOne(ID id) {
@@ -437,7 +418,7 @@ public List<T> findAll(Sort sort) {
437418
@Override
438419
public Page<T> findAll(Pageable pageable) {
439420

440-
if (isUnpaged(pageable)) {
421+
if (pageable.isUnpaged()) {
441422
return new PageImpl<>(findAll());
442423
}
443424

@@ -463,7 +444,7 @@ public List<T> findAll(Specification<T> spec) {
463444
public Page<T> findAll(Specification<T> spec, Pageable pageable) {
464445

465446
TypedQuery<T> query = getQuery(spec, pageable);
466-
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
447+
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList())
467448
: readPage(query, getDomainClass(), pageable, spec);
468449
}
469450

@@ -475,9 +456,12 @@ public List<T> findAll(Specification<T> spec, Sort sort) {
475456
@Override
476457
public boolean exists(Specification<T> spec) {
477458

478-
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class);
479-
cq.select(this.em.getCriteriaBuilder().literal(1));
459+
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
460+
.createQuery(Integer.class) //
461+
.select(this.em.getCriteriaBuilder().literal(1));
462+
480463
applySpecificationToCriteria(spec, getDomainClass(), cq);
464+
481465
TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
482466
return query.setMaxResults(1).getResultList().size() == 1;
483467
}
@@ -565,9 +549,12 @@ public <S extends T> long count(Example<S> example) {
565549
public <S extends T> boolean exists(Example<S> example) {
566550

567551
Specification<S> spec = new ExampleSpecification<>(example, this.escapeCharacter);
568-
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class);
569-
cq.select(this.em.getCriteriaBuilder().literal(1));
552+
CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder() //
553+
.createQuery(Integer.class) //
554+
.select(this.em.getCriteriaBuilder().literal(1));
555+
570556
applySpecificationToCriteria(spec, example.getProbeType(), cq);
557+
571558
TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq));
572559
return query.setMaxResults(1).getResultList().size() == 1;
573560
}
@@ -590,7 +577,7 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
590577
Class<S> probeType = example.getProbeType();
591578
TypedQuery<S> query = getQuery(new ExampleSpecification<>(example, escapeCharacter), probeType, pageable);
592579

593-
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
580+
return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
594581
}
595582

596583
@Override
@@ -804,6 +791,21 @@ protected <S extends T> TypedQuery<Long> getCountQuery(@Nullable Specification<S
804791
return applyRepositoryMethodMetadataForCount(em.createQuery(query));
805792
}
806793

794+
/**
795+
* Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential
796+
* {@link EntityGraph} information.
797+
*/
798+
protected QueryHints getQueryHints() {
799+
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata);
800+
}
801+
802+
/**
803+
* Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries.
804+
*/
805+
protected QueryHints getQueryHintsForCount() {
806+
return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts();
807+
}
808+
807809
/**
808810
* Applies the given {@link Specification} to the given {@link CriteriaQuery}.
809811
*
@@ -854,10 +856,7 @@ private void applyQueryHints(Query query) {
854856
}
855857

856858
getQueryHints().withFetchGraphs(em).forEach(query::setHint);
857-
858-
if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
859-
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
860-
}
859+
applyComment(metadata, query::setHint);
861860
}
862861

863862
private <S> TypedQuery<S> applyRepositoryMethodMetadataForCount(TypedQuery<S> query) {
@@ -878,9 +877,26 @@ private void applyQueryHintsForCount(Query query) {
878877
}
879878

880879
getQueryHintsForCount().forEach(query::setHint);
880+
applyComment(metadata, query::setHint);
881+
}
882+
883+
private Map<String, Object> getHints() {
884+
885+
Map<String, Object> hints = new HashMap<>();
886+
887+
getQueryHints().withFetchGraphs(em).forEach(hints::put);
888+
889+
if (metadata != null) {
890+
applyComment(metadata, hints::put);
891+
}
892+
893+
return hints;
894+
}
895+
896+
private void applyComment(CrudMethodMetadata metadata, BiConsumer<String, Object> consumer) {
881897

882898
if (metadata.getComment() != null && provider.getCommentHintKey() != null) {
883-
query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment()));
899+
consumer.accept(provider.getCommentHintKey(), provider.getCommentHintValue(this.metadata.getComment()));
884900
}
885901
}
886902

@@ -903,10 +919,6 @@ private static long executeCountQuery(TypedQuery<Long> query) {
903919
return total;
904920
}
905921

906-
private static boolean isUnpaged(Pageable pageable) {
907-
return pageable.isUnpaged();
908-
}
909-
910922
/**
911923
* Specification that gives access to the {@link Parameter} instance used to bind the ids for
912924
* {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses

0 commit comments

Comments
 (0)