From e7bc5c3c059da5f1077d8d0cdf98954e949784e3 Mon Sep 17 00:00:00 2001 From: mipo256 Date: Wed, 19 Mar 2025 09:30:17 +0300 Subject: [PATCH 1/2] polishing --- .../model/PersistentEntityIsNewStrategy.java | 22 +++++++++++-------- .../data/repository/query/Parameters.java | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/PersistentEntityIsNewStrategy.java b/src/main/java/org/springframework/data/mapping/model/PersistentEntityIsNewStrategy.java index 22e1573e4b..517463dcb4 100644 --- a/src/main/java/org/springframework/data/mapping/model/PersistentEntityIsNewStrategy.java +++ b/src/main/java/org/springframework/data/mapping/model/PersistentEntityIsNewStrategy.java @@ -41,18 +41,21 @@ class PersistentEntityIsNewStrategy implements IsNewStrategy { * Creates a new {@link PersistentEntityIsNewStrategy} for the given entity. * * @param entity must not be {@literal null}. + * @param idOnly {@code true} if should consider only the ID property of the {@link PersistentEntity}, {@code false} + * if other properties such as {@link PersistentEntity#getVersionProperty() version} shall be + * considered. */ private PersistentEntityIsNewStrategy(PersistentEntity entity, boolean idOnly) { Assert.notNull(entity, "PersistentEntity must not be null"); - this.valueLookup = entity.hasVersionProperty() && !idOnly // - ? source -> entity.getPropertyAccessor(source).getProperty(entity.getRequiredVersionProperty()) - : source -> entity.getIdentifierAccessor(source).getIdentifier(); - - this.valueType = entity.hasVersionProperty() && !idOnly // - ? entity.getRequiredVersionProperty().getType() // - : entity.hasIdProperty() ? entity.getRequiredIdProperty().getType() : null; + if (entity.hasVersionProperty() && !idOnly) { + this.valueLookup = source -> entity.getPropertyAccessor(source).getProperty(entity.getRequiredVersionProperty()); + this.valueType = entity.getRequiredVersionProperty().getType(); + } else { + this.valueLookup = source -> entity.getIdentifierAccessor(source).getIdentifier(); + this.valueType = entity.hasIdProperty() ? entity.getRequiredIdProperty().getType() : null; + } Class type = valueType; @@ -60,8 +63,9 @@ private PersistentEntityIsNewStrategy(PersistentEntity entity, boolean idO if (!ClassUtils.isAssignable(Number.class, type)) { - throw new IllegalArgumentException(String - .format("Only numeric primitives are supported as identifier / version field types; Got: %s", valueType)); + throw new IllegalArgumentException( + String.format("Only numeric primitives are supported as identifier / version field types; Got: %s", + valueType)); } } } diff --git a/src/main/java/org/springframework/data/repository/query/Parameters.java b/src/main/java/org/springframework/data/repository/query/Parameters.java index 1ec43d08fb..5078c7bb23 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameters.java +++ b/src/main/java/org/springframework/data/repository/query/Parameters.java @@ -38,6 +38,8 @@ /** * Abstracts method parameters that have to be bound to query parameters or applied to the query independently. * + * @param The exact type of {@link Parameter} inside {@link Parameters} + * @param The exact type of {@link Parameters} to be used. Generified to allow for recursive generics. * @author Oliver Gierke * @author Christoph Strobl * @author Johannes Englmeier From 70c9789b590bad8c22407c681047d64e2e955509 Mon Sep 17 00:00:00 2001 From: mipo256 Date: Fri, 21 Mar 2025 12:57:02 +0300 Subject: [PATCH 2/2] Exposed the method and repository metadata in the QueryMethod --- .../data/repository/query/QueryMethod.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/src/main/java/org/springframework/data/repository/query/QueryMethod.java index 711c56c486..08ee040bf8 100644 --- a/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -86,7 +86,7 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory this.method = method; this.unwrappedReturnType = potentiallyUnwrapReturnTypeFor(metadata, method); this.metadata = metadata; - this.parameters = createParameters(method, metadata.getDomainTypeInformation()); + this.parameters = createParameters(ParametersSource.of(getMetadata(), method)); this.domainClass = Lazy.of(() -> { @@ -316,11 +316,11 @@ public ResultProcessor getResultProcessor() { return resultProcessor; } - RepositoryMetadata getMetadata() { + public RepositoryMetadata getMetadata() { return metadata; } - Method getMethod() { + public Method getMethod() { return method; } @@ -329,7 +329,7 @@ public String toString() { return method.toString(); } - private static Class potentiallyUnwrapReturnTypeFor(RepositoryMetadata metadata, Method method) { + private static Class potentiallyUnwrapReturnTypeFor(RepositoryMetadata metadata, Method method) { TypeInformation returnType = metadata.getReturnType(method); if (QueryExecutionConverters.supports(returnType.getType())