From 30511fef09ef54b455008e31fca01e73d29e3ea3 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 29 Mar 2023 13:46:22 +0200 Subject: [PATCH] Polishing. See #2878 Original pull request #2885 --- .../jpa/repository/query/CollectionUtils.java | 4 ++-- .../query/JpaKeysetScrollQueryCreator.java | 2 ++ .../repository/query/JpaQueryExecution.java | 1 + .../query/KeysetScrollDelegate.java | 22 +++++++++---------- .../query/KeysetScrollSpecification.java | 1 - .../data/jpa/repository/query/NamedQuery.java | 11 ++++------ .../FetchableFluentQueryByPredicate.java | 4 ++-- .../FetchableFluentQueryBySpecification.java | 4 ++-- .../support/FluentQuerySupport.java | 2 +- .../support/JpaEntityInformation.java | 17 +++++--------- .../JpaMetamodelEntityInformation.java | 6 +---- .../data/jpa/repository/support/Querydsl.java | 8 ------- .../support/QuerydslJpaPredicateExecutor.java | 2 +- .../support/SimpleJpaRepository.java | 2 +- .../data/jpa/domain/sample/Item.java | 1 + 15 files changed, 33 insertions(+), 54 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/CollectionUtils.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/CollectionUtils.java index 582d30eb1..b6bd98085 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/CollectionUtils.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/CollectionUtils.java @@ -30,7 +30,7 @@ class CollectionUtils { * @param count the number of first elements to be included in the returned list. * @param list must not be {@literal null} * @return the returned sublist if the {@code list} is greater {@code count}. - * @param + * @param the element type of the lists. */ public static List getFirst(int count, List list) { @@ -47,7 +47,7 @@ class CollectionUtils { * @param count the number of last elements to be included in the returned list. * @param list must not be {@literal null} * @return the returned sublist if the {@code list} is greater {@code count}. - * @param + * @param the element type of the lists. */ public static List getLast(int count, List list) { diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaKeysetScrollQueryCreator.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaKeysetScrollQueryCreator.java index 5dbf036b4..7ee3d04fa 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaKeysetScrollQueryCreator.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaKeysetScrollQueryCreator.java @@ -45,7 +45,9 @@ class JpaKeysetScrollQueryCreator extends JpaQueryCreator { public JpaKeysetScrollQueryCreator(PartTree tree, ReturnedType type, CriteriaBuilder builder, ParameterMetadataProvider provider, JpaEntityInformation entityInformation, KeysetScrollPosition scrollPosition) { + super(tree, type, builder, provider); + this.entityInformation = entityInformation; this.scrollPosition = scrollPosition; } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java index 618ba586c..90ddbaa67 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java @@ -142,6 +142,7 @@ public abstract class JpaQueryExecution { private final ScrollDelegate delegate; ScrollExecution(Sort sort, ScrollDelegate delegate) { + this.sort = sort; this.delegate = delegate; } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollDelegate.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollDelegate.java index 0ce89ff11..f6061a9c7 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollDelegate.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollDelegate.java @@ -40,8 +40,8 @@ public class KeysetScrollDelegate { /** * Factory method to obtain the right {@link KeysetScrollDelegate}. * - * @param direction - * @return + * @param direction the direction of scrolling. + * @return a {@link KeysetScrollDelegate} matching the requested direction. */ public static KeysetScrollDelegate of(Direction direction) { return direction == Direction.Forward ? forward : reverse; @@ -104,7 +104,6 @@ public class KeysetScrollDelegate { return sort; } - @SuppressWarnings("unchecked") protected List postProcessResults(List result) { return result; } @@ -154,7 +153,6 @@ public class KeysetScrollDelegate { * Create an expression object from the given {@code property} path. * * @param property must not be {@literal null}. - * @return */ E createExpression(String property); @@ -163,8 +161,8 @@ public class KeysetScrollDelegate { * * @param order must not be {@literal null}. * @param propertyExpression must not be {@literal null}. - * @param value - * @return + * @param value the value to compare with. Must not be {@literal null}. + * @return an object representing the comparison predicate. */ P compare(Order order, E propertyExpression, Object value); @@ -172,24 +170,24 @@ public class KeysetScrollDelegate { * Create an equals-comparison object. * * @param propertyExpression must not be {@literal null}. - * @param value - * @return + * @param value the value to compare with. Must not be {@literal null}. + * @return an object representing the comparison predicate. */ P compare(E propertyExpression, @Nullable Object value); /** * AND-combine the {@code intermediate} predicates. * - * @param intermediate - * @return + * @param intermediate the predicates to combine. Must not be {@literal null}. + * @return a single predicate. */ P and(List

intermediate); /** * OR-combine the {@code intermediate} predicates. * - * @param intermediate - * @return + * @param intermediate the predicates to combine. Must not be {@literal null}. + * @return a single predicate. */ P or(List

intermediate); } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollSpecification.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollSpecification.java index cfec4f06d..ee4979a63 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollSpecification.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollSpecification.java @@ -56,7 +56,6 @@ public record KeysetScrollSpecification (KeysetScrollPosition position, Sort * @param position must not be {@literal null}. * @param sort must not be {@literal null}. * @param entity must not be {@literal null}. - * @return */ public static Sort createSort(KeysetScrollPosition position, Sort sort, JpaEntityInformation entity) { diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java index 311f47eaf..e78c9c253 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java @@ -49,7 +49,6 @@ final class NamedQuery extends AbstractJpaQuery { private final String queryName; private final String countQueryName; private final @Nullable String countProjection; - private final QueryExtractor extractor; private final boolean namedCountQueryIsPresent; private final DeclaredQuery declaredQuery; private final QueryParameterSetter.QueryMetadataCache metadataCache; @@ -63,7 +62,7 @@ final class NamedQuery extends AbstractJpaQuery { this.queryName = method.getNamedQueryName(); this.countQueryName = method.getNamedCountQueryName(); - this.extractor = method.getQueryExtractor(); + QueryExtractor extractor = method.getQueryExtractor(); this.countProjection = method.getCountQueryProjection(); Parameters parameters = method.getParameters(); @@ -81,7 +80,7 @@ final class NamedQuery extends AbstractJpaQuery { this.declaredQuery = DeclaredQuery.of(queryString, false); boolean weNeedToCreateCountQuery = !namedCountQueryIsPresent && method.getParameters().hasPageableParameter(); - boolean cantExtractQuery = !this.extractor.canExtractQuery(); + boolean cantExtractQuery = !extractor.canExtractQuery(); if (weNeedToCreateCountQuery && cantExtractQuery) { throw QueryCreationException.create(method, CANNOT_EXTRACT_QUERY); @@ -99,9 +98,8 @@ final class NamedQuery extends AbstractJpaQuery { /** * Returns whether the named query with the given name exists. * - * @param em must not be {@literal null}. + * @param em must not be {@literal null}. * @param queryName must not be {@literal null}. - * @return */ static boolean hasNamedQuery(EntityManager em, String queryName) { @@ -129,8 +127,7 @@ final class NamedQuery extends AbstractJpaQuery { * Looks up a named query for the given {@link org.springframework.data.repository.query.QueryMethod}. * * @param method must not be {@literal null}. - * @param em must not be {@literal null}. - * @return + * @param em must not be {@literal null}. */ @Nullable public static RepositoryQuery lookupFrom(JpaQueryMethod method, EntityManager em) { diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java index 1ac5affde..bba18ff9f 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java @@ -233,9 +233,9 @@ class FetchableFluentQueryByPredicate extends FluentQuerySupport imp static class PredicateScrollDelegate extends ScrollDelegate { - private final ScrollQueryFactory scrollFunction; + private final ScrollQueryFactory scrollFunction; - PredicateScrollDelegate(ScrollQueryFactory scrollQueryFactory, JpaEntityInformation entity) { + PredicateScrollDelegate(ScrollQueryFactory scrollQueryFactory, JpaEntityInformation entity) { super(entity); this.scrollFunction = scrollQueryFactory; } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryBySpecification.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryBySpecification.java index d8193f52a..287f825b7 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryBySpecification.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryBySpecification.java @@ -224,9 +224,9 @@ class FetchableFluentQueryBySpecification extends FluentQuerySupport static class SpecificationScrollDelegate extends ScrollDelegate { - private final ScrollQueryFactory scrollFunction; + private final ScrollQueryFactory scrollFunction; - SpecificationScrollDelegate(ScrollQueryFactory scrollQueryFactory, JpaEntityInformation entity) { + SpecificationScrollDelegate(ScrollQueryFactory scrollQueryFactory, JpaEntityInformation entity) { super(entity); this.scrollFunction = scrollQueryFactory; } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FluentQuerySupport.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FluentQuerySupport.java index 5aa8352ed..4af8f84ee 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FluentQuerySupport.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FluentQuerySupport.java @@ -86,7 +86,7 @@ abstract class FluentQuerySupport { return o -> DefaultConversionService.getSharedInstance().convert(o, targetType); } - interface ScrollQueryFactory { + interface ScrollQueryFactory { Query createQuery(Sort sort, ScrollPosition scrollPosition); } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java index f192386db..d59e6316f 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java @@ -35,8 +35,6 @@ public interface JpaEntityInformation extends EntityInformation, J /** * Returns the id attribute of the entity. - * - * @return */ @Nullable SingularAttribute getIdAttribute(); @@ -62,25 +60,20 @@ public interface JpaEntityInformation extends EntityInformation, J /** * Returns {@literal true} if the entity has a composite id. - * - * @return */ boolean hasCompositeId(); /** * Returns the attribute names of the id attributes. If the entity has a composite id, then all id attribute names are * returned. If the entity has a single id attribute then this single attribute name is returned. - * - * @return */ Collection getIdAttributeNames(); /** * Extracts the value for the given id attribute from a composite id * - * @param id - * @param idAttribute - * @return + * @param id the composite id from which to extract the attribute. + * @param idAttribute the attribute name to extract. */ @Nullable Object getCompositeIdAttributeValue(Object id, String idAttribute); @@ -88,9 +81,9 @@ public interface JpaEntityInformation extends EntityInformation, J /** * Extract a keyset for {@code propertyPaths} and the primary key (including composite key components if applicable). * - * @param propertyPaths - * @param entity - * @return + * @param propertyPaths the property paths that make up the keyset in combination with the composite key components. + * @param entity the entity to extract values from + * @return a map mapping String representations of the paths to values from the entity. * @since 3.1 */ Map getKeyset(Iterable propertyPaths, T entity); diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java index c740887b2..dbf7285a0 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java @@ -85,12 +85,10 @@ public class JpaMetamodelEntityInformation extends JpaEntityInformationSu this.entityName = type instanceof EntityType ? ((EntityType) type).getName() : null; - if (!(type instanceof IdentifiableType)) { + if (!(type instanceof IdentifiableType identifiableType)) { throw new IllegalArgumentException("The given domain class does not contain an id attribute"); } - IdentifiableType identifiableType = (IdentifiableType) type; - this.idMetadata = new IdMetadata<>(identifiableType, PersistenceProvider.fromMetamodel(metamodel)); this.versionAttribute = findVersionAttribute(identifiableType, metamodel); @@ -108,7 +106,6 @@ public class JpaMetamodelEntityInformation extends JpaEntityInformationSu * * @param type must not be {@literal null}. * @param metamodel must not be {@literal null}. - * @return */ @SuppressWarnings("unchecked") private static Optional> findVersionAttribute(IdentifiableType type, @@ -262,7 +259,6 @@ public class JpaMetamodelEntityInformation extends JpaEntityInformationSu private final Set> attributes; private @Nullable Class idType; - @SuppressWarnings("unchecked") IdMetadata(IdentifiableType source, PersistenceProvider persistenceProvider) { this.type = source; diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java index 2e38c4c2b..25fb4abec 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java @@ -73,8 +73,6 @@ public class Querydsl { /** * Creates the {@link JPQLQuery} instance based on the configured {@link EntityManager}. - * - * @return */ public AbstractJPAQuery> createQuery() { @@ -93,7 +91,6 @@ public class Querydsl { * Creates the {@link JPQLQuery} instance based on the configured {@link EntityManager}. * * @param paths must not be {@literal null}. - * @return */ public AbstractJPAQuery> createQuery(EntityPath... paths) { @@ -167,7 +164,6 @@ public class Querydsl { * * @param sort must not be {@literal null}. * @param query must not be {@literal null}. - * @return */ private JPQLQuery addOrderByFrom(Sort sort, JPQLQuery query) { @@ -185,7 +181,6 @@ public class Querydsl { * Transforms a plain {@link Order} into a QueryDsl specific {@link OrderSpecifier}. * * @param order must not be {@literal null}. - * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private OrderSpecifier toOrderSpecifier(Order order) { @@ -200,7 +195,6 @@ public class Querydsl { * {@link NullHandling}. * * @param nullHandling must not be {@literal null}. - * @return * @since 1.6 */ private NullHandling toQueryDslNullHandling(org.springframework.data.domain.Sort.NullHandling nullHandling) { @@ -225,7 +219,6 @@ public class Querydsl { * Creates an {@link Expression} for the given {@link Order} property. * * @param order must not be {@literal null}. - * @return */ private Expression buildOrderPropertyPathFrom(Order order) { @@ -250,7 +243,6 @@ public class Querydsl { * Creates an {@link Expression} for the given {@code property} property. * * @param property must not be {@literal null}. - * @return */ Expression createExpression(String property) { diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java index c899d051a..1cfcd69be 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java @@ -175,7 +175,7 @@ public class QuerydslJpaPredicateExecutor implements QuerydslPredicateExecuto return select; }; - ScrollQueryFactory scroll = (sort, scrollPosition) -> { + ScrollQueryFactory scroll = (sort, scrollPosition) -> { Predicate predicateToUse = predicate; 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 788ea6fae..5358af188 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 @@ -521,7 +521,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation scrollFunction = (sort, scrollPosition) -> { + ScrollQueryFactory scrollFunction = (sort, scrollPosition) -> { Specification specToUse = spec; diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Item.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Item.java index 6fce01ece..38c6b83b0 100755 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Item.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Item.java @@ -50,6 +50,7 @@ public class Item { } public Item(Integer id, Integer manufacturerId, String name) { + this.id = id; this.manufacturerId = manufacturerId; this.name = name;