From 48cb33805bf56fb2b39019a02609ca3e7e4429ac Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 2 Jun 2023 14:35:31 +0200 Subject: [PATCH] Polishing. Reuse comment hint retrieval. Reorder methods. Inline methods and use fluent API where possible. See #2991 Original pull request: #2995 --- .../support/SimpleJpaRepository.java | 92 +++++++++++-------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index f48d9c3bcd..e9a2d4b204 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -38,6 +38,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -230,9 +231,11 @@ public void deleteAllByIdInBatch(Iterable ids) { entityInformation.getIdAttribute().getName()); Query query = em.createQuery(queryString); - /** + + /* * Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already. */ + if (Collection.class.isInstance(ids)) { query.setParameter("ids", ids); } else { @@ -304,33 +307,11 @@ public Optional findById(ID id) { } LockModeType type = metadata.getLockModeType(); - - Map hints = new HashMap<>(); - - getQueryHints().withFetchGraphs(em).forEach(hints::put); - - if (metadata != null && metadata.getComment() != null && provider.getCommentHintKey() != null) { - hints.put(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment())); - } + Map hints = getHints(); return Optional.ofNullable(type == null ? em.find(domainType, id, hints) : em.find(domainType, id, type, hints)); } - /** - * Returns {@link QueryHints} with the query hints based on the current {@link CrudMethodMetadata} and potential - * {@link EntityGraph} information. - */ - protected QueryHints getQueryHints() { - return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata); - } - - /** - * Returns {@link QueryHints} with the query hints on the current {@link CrudMethodMetadata} for count queries. - */ - protected QueryHints getQueryHintsForCount() { - return metadata == null ? NoHints.INSTANCE : DefaultQueryHints.of(entityInformation, metadata).forCounts(); - } - @Deprecated @Override public T getOne(ID id) { @@ -437,7 +418,7 @@ public List findAll(Sort sort) { @Override public Page findAll(Pageable pageable) { - if (isUnpaged(pageable)) { + if (pageable.isUnpaged()) { return new PageImpl<>(findAll()); } @@ -463,7 +444,7 @@ public List findAll(Specification spec) { public Page findAll(Specification spec, Pageable pageable) { TypedQuery query = getQuery(spec, pageable); - return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) + return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPage(query, getDomainClass(), pageable, spec); } @@ -475,9 +456,12 @@ public List findAll(Specification spec, Sort sort) { @Override public boolean exists(Specification spec) { - CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery(Integer.class); - cq.select(this.em.getCriteriaBuilder().literal(1)); + CriteriaQuery cq = this.em.getCriteriaBuilder() // + .createQuery(Integer.class) // + .select(this.em.getCriteriaBuilder().literal(1)); + applySpecificationToCriteria(spec, getDomainClass(), cq); + TypedQuery query = applyRepositoryMethodMetadata(this.em.createQuery(cq)); return query.setMaxResults(1).getResultList().size() == 1; } @@ -565,9 +549,12 @@ public long count(Example example) { public boolean exists(Example example) { Specification spec = new ExampleSpecification<>(example, this.escapeCharacter); - CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery(Integer.class); - cq.select(this.em.getCriteriaBuilder().literal(1)); + CriteriaQuery cq = this.em.getCriteriaBuilder() // + .createQuery(Integer.class) // + .select(this.em.getCriteriaBuilder().literal(1)); + applySpecificationToCriteria(spec, example.getProbeType(), cq); + TypedQuery query = applyRepositoryMethodMetadata(this.em.createQuery(cq)); return query.setMaxResults(1).getResultList().size() == 1; } @@ -590,7 +577,7 @@ public Page findAll(Example example, Pageable pageable) { Class probeType = example.getProbeType(); TypedQuery query = getQuery(new ExampleSpecification<>(example, escapeCharacter), probeType, pageable); - return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); + return pageable.isUnpaged() ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); } @Override @@ -804,6 +791,21 @@ protected TypedQuery getCountQuery(@Nullable Specification TypedQuery applyRepositoryMethodMetadataForCount(TypedQuery query) { @@ -878,9 +877,26 @@ private void applyQueryHintsForCount(Query query) { } getQueryHintsForCount().forEach(query::setHint); + applyComment(metadata, query::setHint); + } + + private Map getHints() { + + Map hints = new HashMap<>(); + + getQueryHints().withFetchGraphs(em).forEach(hints::put); + + if (metadata != null) { + applyComment(metadata, hints::put); + } + + return hints; + } + + private void applyComment(CrudMethodMetadata metadata, BiConsumer consumer) { if (metadata.getComment() != null && provider.getCommentHintKey() != null) { - query.setHint(provider.getCommentHintKey(), provider.getCommentHintValue(metadata.getComment())); + consumer.accept(provider.getCommentHintKey(), provider.getCommentHintValue(this.metadata.getComment())); } } @@ -903,10 +919,6 @@ private static long executeCountQuery(TypedQuery query) { return total; } - private static boolean isUnpaged(Pageable pageable) { - return pageable.isUnpaged(); - } - /** * Specification that gives access to the {@link Parameter} instance used to bind the ids for * {@link SimpleJpaRepository#findAllById(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses