Skip to content

Commit 468f94b

Browse files
mipo256schauder
authored andcommitted
Avoid superfluous creation of RowMappers.
For modifying queries RowMappers get now longer created. Improved documentation for `@Query` annotation. Original pull request #1423
1 parent 761f008 commit 468f94b

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ public JdbcQueryMethod getQueryMethod() {
8282
protected JdbcQueryExecution<?> getQueryExecution(JdbcQueryMethod queryMethod,
8383
@Nullable ResultSetExtractor<?> extractor, RowMapper<?> rowMapper) {
8484

85-
if (queryMethod.isModifyingQuery()) {
86-
return createModifyingQueryExecutor();
87-
}
88-
8985
if (queryMethod.isCollectionQuery()) {
9086
return extractor != null ? getQueryExecution(extractor) : collectionQuery(rowMapper);
9187
}
@@ -97,7 +93,7 @@ protected JdbcQueryExecution<?> getQueryExecution(JdbcQueryMethod queryMethod,
9793
return extractor != null ? getQueryExecution(extractor) : singleObjectQuery(rowMapper);
9894
}
9995

100-
private JdbcQueryExecution<Object> createModifyingQueryExecutor() {
96+
protected JdbcQueryExecution<Object> createModifyingQueryExecutor() {
10197

10298
return (query, parameters) -> {
10399

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.jdbc.core.RowMapper;
4444
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4545
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
46+
import org.springframework.lang.Nullable;
4647
import org.springframework.util.Assert;
4748

4849
/**
@@ -144,9 +145,7 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
144145
resultProcessingConverter);
145146
}
146147

147-
JdbcQueryExecution<?> queryExecution = getQueryMethod().isPageQuery() || getQueryMethod().isSliceQuery()
148-
? collectionQuery(rowMapper)
149-
: getQueryExecution(getQueryMethod(), extractor, rowMapper);
148+
JdbcQueryExecution<?> queryExecution = getJdbcQueryExecution(extractor, rowMapper);
150149

151150
if (getQueryMethod().isSliceQuery()) {
152151
return new SliceQueryExecution<>((JdbcQueryExecution<Collection<Object>>) queryExecution, accessor.getPageable());
@@ -173,6 +172,18 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
173172
return queryExecution;
174173
}
175174

175+
private JdbcQueryExecution<?> getJdbcQueryExecution(@Nullable ResultSetExtractor<Boolean> extractor, RowMapper<Object> rowMapper) {
176+
if (getQueryMethod().isPageQuery() || getQueryMethod().isSliceQuery()) {
177+
return collectionQuery(rowMapper);
178+
} else {
179+
if (getQueryMethod().isModifyingQuery()) {
180+
return createModifyingQueryExecutor();
181+
} else {
182+
return getQueryExecution(getQueryMethod(), extractor, rowMapper);
183+
}
184+
}
185+
}
186+
176187
protected ParametrizedQuery createQuery(RelationalParametersParameterAccessor accessor, ReturnedType returnedType) {
177188

178189
RelationalEntityMetadata<?> entityMetadata = getQueryMethod().getEntityInformation();

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
* Annotation to provide SQL statements that will get used for executing the method. The SQL statement may contain named
3030
* parameters as supported by {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. Those
3131
* parameters will get bound to the arguments of the annotated method.
32+
* <p>
33+
* You can also specify the way to extract data from {@link java.sql.ResultSet}. There are 4 attribute of this
34+
* annotation you can set to do that:
35+
* <p>
36+
* 1. {@link #resultSetExtractorRef()}
37+
* 2. {@link #resultSetExtractorClass()}
38+
* 3. {@link #rowMapperRef()}
39+
* 4. {@link #rowMapperClass()}
40+
*
41+
* The annotation attributes above are listed in their preference order, that is - the {@link #resultSetExtractorRef()},
42+
* has the highest privilege and, will suppress any other 3 attribute from above, and consequently {@link #rowMapperClass()}
43+
* has the lowest privilege and will be used if any of three above are not specified.
3244
*
3345
* @author Jens Schauder
3446
* @author Moises Cisneros
@@ -52,28 +64,23 @@
5264
String name() default "";
5365

5466
/**
55-
* Optional {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
56-
* along with {@link #resultSetExtractorClass()} only one of the two can be set.
67+
* Optional {@link RowMapper} to use to convert the result of the query to domain class instances.
5768
*/
5869
Class<? extends RowMapper> rowMapperClass() default RowMapper.class;
5970

6071
/**
61-
* Optional name of a bean of type {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
62-
* along with {@link #resultSetExtractorClass()} only one of the two can be set.
63-
*
72+
* Optional name of a bean of type {@link RowMapper} to use to convert the result of the query to domain class instances.
6473
* @since 2.1
6574
*/
6675
String rowMapperRef() default "";
6776

6877
/**
69-
* Optional {@link ResultSetExtractor} to use to convert the result of the query to domain class instances. Cannot be
70-
* used along with {@link #rowMapperClass()} only one of the two can be set.
78+
* Optional {@link ResultSetExtractor} to use to convert the result of the query to domain class instances.
7179
*/
7280
Class<? extends ResultSetExtractor> resultSetExtractorClass() default ResultSetExtractor.class;
7381

7482
/**
75-
* Optional name of a bean of type {@link ResultSetExtractor} to use to convert the result of the query to domain class instances. Cannot be
76-
* used along with {@link #rowMapperClass()} only one of the two can be set.
83+
* Optional name of a bean of type {@link ResultSetExtractor} to use to convert the result of the query to domain class instances.
7784
*
7885
* @since 2.1
7986
*/

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,7 @@ public Object execute(Object[] objects) {
128128
ResultProcessingConverter converter = new ResultProcessingConverter(processor, this.converter.getMappingContext(),
129129
this.converter.getEntityInstantiators());
130130

131-
RowMapper<Object> rowMapper = determineRowMapper(rowMapperFactory.create(resolveTypeToRead(processor)), converter,
132-
accessor.findDynamicProjection() != null);
133-
134-
JdbcQueryExecution<?> queryExecution = getQueryExecution(//
135-
queryMethod, //
136-
determineResultSetExtractor(rowMapper), //
137-
rowMapper);
131+
JdbcQueryExecution<?> queryExecution = createJdbcQueryExecution(accessor, processor, converter);
138132

139133
MapSqlParameterSource parameterMap = this.bindParameters(accessor);
140134

@@ -147,6 +141,21 @@ public Object execute(Object[] objects) {
147141
return queryExecution.execute(processSpelExpressions(objects, parameterMap, query), parameterMap);
148142
}
149143

144+
private JdbcQueryExecution<?> createJdbcQueryExecution(RelationalParameterAccessor accessor, ResultProcessor processor, ResultProcessingConverter converter) {
145+
JdbcQueryExecution<?> queryExecution;
146+
147+
if (queryMethod.isModifyingQuery()) {
148+
queryExecution = createModifyingQueryExecutor();
149+
} else {
150+
151+
RowMapper<Object> rowMapper = determineRowMapper(rowMapperFactory.create(resolveTypeToRead(processor)), converter,
152+
accessor.findDynamicProjection() != null);
153+
154+
queryExecution = getQueryExecution(queryMethod, determineResultSetExtractor(rowMapper), rowMapper);
155+
}
156+
return queryExecution;
157+
}
158+
150159
private String processSpelExpressions(Object[] objects, MapSqlParameterSource parameterMap, String query) {
151160

152161
SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext

0 commit comments

Comments
 (0)