16
16
package org .springframework .data .jdbc .repository .support ;
17
17
18
18
import java .lang .reflect .Method ;
19
- import java .sql .ResultSet ;
20
- import java .sql .SQLException ;
21
19
22
20
import org .apache .commons .logging .Log ;
23
21
import org .apache .commons .logging .LogFactory ;
24
22
import org .springframework .beans .factory .BeanFactory ;
25
23
import org .springframework .context .ApplicationEventPublisher ;
26
- import org .springframework .data .jdbc .core .convert .EntityRowMapper ;
27
24
import org .springframework .data .jdbc .core .convert .JdbcConverter ;
28
25
import org .springframework .data .jdbc .repository .QueryMappingConfiguration ;
29
- import org .springframework .data .jdbc .repository .query .AbstractJdbcQuery ;
26
+ import org .springframework .data .jdbc .repository .query .DefaultRowMapperFactory ;
30
27
import org .springframework .data .jdbc .repository .query .JdbcQueryMethod ;
31
28
import org .springframework .data .jdbc .repository .query .PartTreeJdbcQuery ;
29
+ import org .springframework .data .jdbc .repository .query .RowMapperFactory ;
32
30
import org .springframework .data .jdbc .repository .query .StringBasedJdbcQuery ;
33
31
import org .springframework .data .mapping .callback .EntityCallbacks ;
34
32
import org .springframework .data .projection .ProjectionFactory ;
35
33
import org .springframework .data .relational .core .dialect .Dialect ;
36
34
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
37
- import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
38
- import org .springframework .data .relational .core .mapping .event .AfterConvertCallback ;
39
- import org .springframework .data .relational .core .mapping .event .AfterConvertEvent ;
40
35
import org .springframework .data .relational .repository .support .RelationalQueryLookupStrategy ;
41
36
import org .springframework .data .repository .core .NamedQueries ;
42
37
import org .springframework .data .repository .core .RepositoryMetadata ;
43
38
import org .springframework .data .repository .query .QueryLookupStrategy ;
44
39
import org .springframework .data .repository .query .RepositoryQuery ;
45
40
import org .springframework .data .repository .query .ValueExpressionDelegate ;
46
- import org .springframework .jdbc .core .ResultSetExtractor ;
47
- import org .springframework .jdbc .core .RowMapper ;
48
- import org .springframework .jdbc .core .SingleColumnRowMapper ;
49
41
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
50
42
import org .springframework .lang .Nullable ;
51
43
import org .springframework .util .Assert ;
62
54
* @author Hebert Coelho
63
55
* @author Diego Krupitza
64
56
* @author Christopher Klein
57
+ * @author Mikhail Polivakha
65
58
*/
66
59
abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
67
60
@@ -109,13 +102,17 @@ public RelationalMappingContext getMappingContext() {
109
102
*/
110
103
static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy {
111
104
105
+ private final RowMapperFactory rowMapperFactory ;
106
+
112
107
CreateQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
113
108
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
114
109
QueryMappingConfiguration queryMappingConfiguration , NamedParameterJdbcOperations operations ,
115
110
ValueExpressionDelegate delegate ) {
116
111
117
112
super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations ,
118
113
delegate );
114
+
115
+ this .rowMapperFactory = new DefaultRowMapperFactory (getMappingContext (), getConverter (), getQueryMappingConfiguration (), getCallbacks (), getPublisher ());
119
116
}
120
117
121
118
@ Override
@@ -124,8 +121,7 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
124
121
125
122
JdbcQueryMethod queryMethod = getJdbcQueryMethod (method , repositoryMetadata , projectionFactory , namedQueries );
126
123
127
- return new PartTreeJdbcQuery (getMappingContext (), queryMethod , getDialect (), getConverter (), getOperations (),
128
- this ::createMapper );
124
+ return new PartTreeJdbcQuery (getMappingContext (), queryMethod , getDialect (), getConverter (), getOperations (), rowMapperFactory );
129
125
}
130
126
}
131
127
@@ -138,7 +134,7 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
138
134
*/
139
135
static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy {
140
136
141
- private final AbstractJdbcQuery . RowMapperFactory rowMapperFactory ;
137
+ private final RowMapperFactory rowMapperFactory ;
142
138
143
139
DeclaredQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
144
140
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
@@ -147,7 +143,7 @@ static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy {
147
143
super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations ,
148
144
delegate );
149
145
150
- this .rowMapperFactory = new BeanFactoryRowMapperFactory ( beanfactory );
146
+ this .rowMapperFactory = new BeanFactoryAwareRowMapperFactory ( context , converter , queryMappingConfiguration , callbacks , publisher , beanfactory );
151
147
}
152
148
153
149
@ Override
@@ -172,44 +168,6 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
172
168
throw new IllegalStateException (
173
169
String .format ("Did neither find a NamedQuery nor an annotated query for method %s" , method ));
174
170
}
175
-
176
- @ SuppressWarnings ("unchecked" )
177
- private class BeanFactoryRowMapperFactory implements AbstractJdbcQuery .RowMapperFactory {
178
-
179
- private final @ Nullable BeanFactory beanFactory ;
180
-
181
- BeanFactoryRowMapperFactory (@ Nullable BeanFactory beanFactory ) {
182
- this .beanFactory = beanFactory ;
183
- }
184
-
185
- @ Override
186
- public RowMapper <Object > create (Class <?> result ) {
187
- return createMapper (result );
188
- }
189
-
190
- @ Override
191
- public RowMapper <Object > getRowMapper (String reference ) {
192
-
193
- if (beanFactory == null ) {
194
- throw new IllegalStateException (
195
- "Cannot resolve RowMapper bean reference '" + reference + "'; BeanFactory is not configured." );
196
- }
197
-
198
- return beanFactory .getBean (reference , RowMapper .class );
199
- }
200
-
201
- @ Override
202
- public ResultSetExtractor <Object > getResultSetExtractor (String reference ) {
203
-
204
- if (beanFactory == null ) {
205
- throw new IllegalStateException (
206
- "Cannot resolve ResultSetExtractor bean reference '" + reference + "'; BeanFactory is not configured." );
207
- }
208
-
209
- return beanFactory .getBean (reference , ResultSetExtractor .class );
210
- }
211
- }
212
-
213
171
}
214
172
215
173
/**
@@ -320,57 +278,15 @@ NamedParameterJdbcOperations getOperations() {
320
278
return operations ;
321
279
}
322
280
323
- @ SuppressWarnings ("unchecked" )
324
- RowMapper <Object > createMapper (Class <?> returnedObjectType ) {
325
-
326
- RelationalPersistentEntity <?> persistentEntity = getMappingContext ().getPersistentEntity (returnedObjectType );
327
-
328
- if (persistentEntity == null ) {
329
- return (RowMapper <Object >) SingleColumnRowMapper .newInstance (returnedObjectType ,
330
- converter .getConversionService ());
331
- }
332
-
333
- return (RowMapper <Object >) determineDefaultMapper (returnedObjectType );
334
- }
335
-
336
- private RowMapper <?> determineDefaultMapper (Class <?> returnedObjectType ) {
337
-
338
- RowMapper <?> configuredQueryMapper = queryMappingConfiguration .getRowMapper (returnedObjectType );
339
-
340
- if (configuredQueryMapper != null )
341
- return configuredQueryMapper ;
342
-
343
- EntityRowMapper <?> defaultEntityRowMapper = new EntityRowMapper <>( //
344
- getMappingContext ().getRequiredPersistentEntity (returnedObjectType ), //
345
- converter //
346
- );
347
-
348
- return new PostProcessingRowMapper <>(defaultEntityRowMapper );
349
- }
350
-
351
- class PostProcessingRowMapper <T > implements RowMapper <T > {
352
-
353
- private final RowMapper <T > delegate ;
281
+ QueryMappingConfiguration getQueryMappingConfiguration () {
282
+ return queryMappingConfiguration ;
283
+ }
354
284
355
- PostProcessingRowMapper (RowMapper <T > delegate ) {
356
- this .delegate = delegate ;
357
- }
358
-
359
- @ Override
360
- public T mapRow (ResultSet rs , int rowNum ) throws SQLException {
361
-
362
- T entity = delegate .mapRow (rs , rowNum );
363
-
364
- if (entity != null ) {
365
-
366
- publisher .publishEvent (new AfterConvertEvent <>(entity ));
285
+ EntityCallbacks getCallbacks () {
286
+ return callbacks ;
287
+ }
367
288
368
- if (callbacks != null ) {
369
- return callbacks .callback (AfterConvertCallback .class , entity );
370
- }
371
- }
372
-
373
- return entity ;
374
- }
375
- }
289
+ ApplicationEventPublisher getPublisher () {
290
+ return publisher ;
291
+ }
376
292
}
0 commit comments