Skip to content

Commit 764a3ed

Browse files
committed
Polishing.
Remove unused parameter from DeclaredQuery. Reformat code. See: #3293 Original pull request: #3339
1 parent 93385c7 commit 764a3ed

File tree

6 files changed

+19
-28
lines changed

6 files changed

+19
-28
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import jakarta.persistence.EntityManager;
1919
import jakarta.persistence.Query;
20+
2021
import org.springframework.data.domain.Pageable;
2122
import org.springframework.data.domain.Sort;
2223
import org.springframework.data.jpa.repository.QueryRewriter;
@@ -80,16 +81,17 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
8081

8182
this.countQuery = Lazy.of(() -> {
8283

83-
if(StringUtils.hasText(countQueryString)) {
84+
if (StringUtils.hasText(countQueryString)) {
8485

8586
return new ExpressionBasedStringQuery(countQueryString, method.getEntityInformation(), parser,
8687
method.isNativeQuery());
8788
}
88-
return query.deriveCountQuery(null, method.getCountQueryProjection());
89+
90+
return query.deriveCountQuery(method.getCountQueryProjection());
8991
});
9092

9193
this.countParameterBinder = Lazy.of(() -> {
92-
return this.createCountBinder(this.countQuery.get());
94+
return this.createBinder(this.countQuery.get());
9395
});
9496

9597
this.parser = parser;
@@ -118,15 +120,14 @@ public Query doCreateQuery(JpaParametersParameterAccessor accessor) {
118120

119121
@Override
120122
protected ParameterBinder createBinder() {
123+
return createBinder(query);
124+
}
121125

126+
protected ParameterBinder createBinder(DeclaredQuery query) {
122127
return ParameterBinderFactory.createQueryAwareBinder(getQueryMethod().getParameters(), query, parser,
123128
evaluationContextProvider);
124129
}
125130

126-
protected ParameterBinder createCountBinder(DeclaredQuery countQuery) {
127-
return ParameterBinderFactory.createQueryAwareBinder(getQueryMethod().getParameters(), countQuery, parser, evaluationContextProvider);
128-
}
129-
130131
@Override
131132
protected Query doCreateCountQuery(JpaParametersParameterAccessor accessor) {
132133

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ static DeclaredQuery of(@Nullable String query, boolean nativeQuery) {
8080
* expected from the original query, either derived from the query wrapped by this instance or from the information
8181
* passed as arguments.
8282
*
83-
* @param countQuery an optional query string to be used if present.
8483
* @param countQueryProjection an optional return type for the query.
8584
* @return a new {@literal DeclaredQuery} instance.
8685
*/
87-
DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection);
86+
DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection);
8887

8988
/**
9089
* @return whether paging is implemented in the query itself, e.g. using SpEL expressions.

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyDeclaredQuery.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.List;
2020

2121
import org.springframework.lang.Nullable;
22-
import org.springframework.util.Assert;
2322

2423
/**
2524
* NULL-Object pattern implementation for {@link DeclaredQuery}.
@@ -65,11 +64,8 @@ public List<ParameterBinding> getParameterBindings() {
6564
}
6665

6766
@Override
68-
public DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection) {
69-
70-
Assert.hasText(countQuery, "CountQuery must not be empty");
71-
72-
return DeclaredQuery.of(countQuery, false);
67+
public DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection) {
68+
return EMPTY_QUERY;
7369
}
7470

7571
@Override

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private NamedQuery(JpaQueryMethod method, EntityManager em) {
9898
/**
9999
* Returns whether the named query with the given name exists.
100100
*
101-
* @param em must not be {@literal null}.
101+
* @param em must not be {@literal null}.
102102
* @param queryName must not be {@literal null}.
103103
*/
104104
static boolean hasNamedQuery(EntityManager em, String queryName) {
@@ -127,7 +127,7 @@ static boolean hasNamedQuery(EntityManager em, String queryName) {
127127
* Looks up a named query for the given {@link org.springframework.data.repository.query.QueryMethod}.
128128
*
129129
* @param method must not be {@literal null}.
130-
* @param em must not be {@literal null}.
130+
* @param em must not be {@literal null}.
131131
*/
132132
@Nullable
133133
public static RepositoryQuery lookupFrom(JpaQueryMethod method, EntityManager em) {
@@ -190,7 +190,7 @@ protected TypedQuery<Long> doCreateCountQuery(JpaParametersParameterAccessor acc
190190

191191
} else {
192192

193-
String countQueryString = declaredQuery.deriveCountQuery(null, countProjection).getQueryString();
193+
String countQueryString = declaredQuery.deriveCountQuery(countProjection).getQueryString();
194194
cacheKey = countQueryString;
195195
countQuery = em.createQuery(countQueryString, Long.class);
196196
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class StringQuery implements DeclaredQuery {
7272
*
7373
* @param query must not be {@literal null} or empty.
7474
*/
75-
@SuppressWarnings("deprecation")
7675
StringQuery(String query, boolean isNative) {
7776

7877
Assert.hasText(query, "Query must not be null or empty");
@@ -109,16 +108,12 @@ public List<ParameterBinding> getParameterBindings() {
109108
}
110109

111110
@Override
112-
public DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection) {
113-
114-
if(StringUtils.hasText(countQuery)) {
115-
return new StringQuery(countQuery, this.isNative);
116-
}
111+
public DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection) {
117112

118113
StringQuery stringQuery = new StringQuery(this.queryEnhancer.createCountQueryFor(countQueryProjection), //
119114
this.isNative);
120115

121-
if(this.hasParameterBindings() && !this.getParameterBindings().equals(stringQuery.getParameterBindings())) {
116+
if (this.hasParameterBindings() && !this.getParameterBindings().equals(stringQuery.getParameterBindings())) {
122117
stringQuery.getParameterBindings().clear();
123118
stringQuery.getParameterBindings().addAll(this.bindings);
124119
}
@@ -289,7 +284,6 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
289284
parameterIndex = expressionParameterIndex;
290285
}
291286

292-
293287
BindingIdentifier queryParameter;
294288
if (parameterIndex != null) {
295289
queryParameter = BindingIdentifier.of(parameterIndex);

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,9 @@ Window<User> findTop3ByFirstnameStartingWithOrderByFirstnameAscEmailAddressAsc(S
446446
@Query("select u from User u where u.firstname = ?#{[0]} and u.firstname = ?1 and u.lastname like %?#{[1]}% and u.lastname like %?2%")
447447
List<User> findByFirstnameAndLastnameWithSpelExpression(String firstname, String lastname);
448448

449-
@Query(value = "select * from SD_User", countQuery = "select count(1) from SD_User u where u.lastname = :#{#lastname}", nativeQuery = true)
450-
Page<User> findByWithSpelParameterOnlyUsedForCountQuery(String lastname, Pageable page);
449+
@Query(value = "select * from SD_User",
450+
countQuery = "select count(1) from SD_User u where u.lastname = :#{#lastname}", nativeQuery = true)
451+
Page<User> findByWithSpelParameterOnlyUsedForCountQuery(@Param("lastname") String lastname, Pageable page);
451452

452453
// DATAJPA-564
453454
@Query("select u from User u where u.lastname like %:#{[0]}% and u.lastname like %:lastname%")

0 commit comments

Comments
 (0)