60
60
* @author Radim Tlusty
61
61
* @author Chirag Tailor
62
62
* @author Diego Krupitza
63
+ * @author Mikhail Polivakha
63
64
* @since 1.1
64
65
*/
65
66
public class DefaultDataAccessStrategy implements DataAccessStrategy {
@@ -71,6 +72,8 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
71
72
private final SqlParametersFactory sqlParametersFactory ;
72
73
private final InsertStrategyFactory insertStrategyFactory ;
73
74
75
+ private final QueryMappingConfiguration queryMappingConfiguration ;
76
+
74
77
/**
75
78
* Creates a {@link DefaultDataAccessStrategy}
76
79
*
@@ -82,21 +85,23 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
82
85
*/
83
86
public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , RelationalMappingContext context ,
84
87
JdbcConverter converter , NamedParameterJdbcOperations operations , SqlParametersFactory sqlParametersFactory ,
85
- InsertStrategyFactory insertStrategyFactory ) {
88
+ InsertStrategyFactory insertStrategyFactory , QueryMappingConfiguration queryMappingConfiguration ) {
86
89
87
90
Assert .notNull (sqlGeneratorSource , "SqlGeneratorSource must not be null" );
88
91
Assert .notNull (context , "RelationalMappingContext must not be null" );
89
92
Assert .notNull (converter , "JdbcConverter must not be null" );
90
93
Assert .notNull (operations , "NamedParameterJdbcOperations must not be null" );
91
94
Assert .notNull (sqlParametersFactory , "SqlParametersFactory must not be null" );
92
95
Assert .notNull (insertStrategyFactory , "InsertStrategyFactory must not be null" );
96
+ Assert .notNull (queryMappingConfiguration , "InsertStrategyFactory must not be null" );
93
97
94
98
this .sqlGeneratorSource = sqlGeneratorSource ;
95
99
this .context = context ;
96
100
this .converter = converter ;
97
101
this .operations = operations ;
98
102
this .sqlParametersFactory = sqlParametersFactory ;
99
103
this .insertStrategyFactory = insertStrategyFactory ;
104
+ this .queryMappingConfiguration = queryMappingConfiguration ;
100
105
}
101
106
102
107
@ Override
@@ -265,15 +270,15 @@ public <T> T findById(Object id, Class<T> domainType) {
265
270
SqlIdentifierParameterSource parameter = sqlParametersFactory .forQueryById (id , domainType , ID_SQL_PARAMETER );
266
271
267
272
try {
268
- return operations .queryForObject (findOneSql , parameter , getEntityRowMapper (domainType ));
273
+ return operations .queryForObject (findOneSql , parameter , getRowMapper (domainType ));
269
274
} catch (EmptyResultDataAccessException e ) {
270
275
return null ;
271
276
}
272
277
}
273
278
274
279
@ Override
275
280
public <T > List <T > findAll (Class <T > domainType ) {
276
- return operations .query (sql (domainType ).getFindAll (), getEntityRowMapper (domainType ));
281
+ return operations .query (sql (domainType ).getFindAll (), getRowMapper (domainType ));
277
282
}
278
283
279
284
@ Override
@@ -285,7 +290,7 @@ public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {
285
290
286
291
SqlParameterSource parameterSource = sqlParametersFactory .forQueryByIds (ids , domainType );
287
292
String findAllInListSql = sql (domainType ).getFindAllInList ();
288
- return operations .query (findAllInListSql , parameterSource , getEntityRowMapper (domainType ));
293
+ return operations .query (findAllInListSql , parameterSource , getRowMapper (domainType ));
289
294
}
290
295
291
296
@ Override
@@ -339,12 +344,12 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
339
344
340
345
@ Override
341
346
public <T > List <T > findAll (Class <T > domainType , Sort sort ) {
342
- return operations .query (sql (domainType ).getFindAll (sort ), getEntityRowMapper (domainType ));
347
+ return operations .query (sql (domainType ).getFindAll (sort ), getRowMapper (domainType ));
343
348
}
344
349
345
350
@ Override
346
351
public <T > List <T > findAll (Class <T > domainType , Pageable pageable ) {
347
- return operations .query (sql (domainType ).getFindAll (pageable ), getEntityRowMapper (domainType ));
352
+ return operations .query (sql (domainType ).getFindAll (pageable ), getRowMapper (domainType ));
348
353
}
349
354
350
355
@ Override
@@ -354,7 +359,7 @@ public <T> Optional<T> findOne(Query query, Class<T> domainType) {
354
359
String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource );
355
360
356
361
try {
357
- return Optional .ofNullable (operations .queryForObject (sqlQuery , parameterSource , getEntityRowMapper (domainType )));
362
+ return Optional .ofNullable (operations .queryForObject (sqlQuery , parameterSource , getRowMapper (domainType )));
358
363
} catch (EmptyResultDataAccessException e ) {
359
364
return Optional .empty ();
360
365
}
@@ -366,7 +371,7 @@ public <T> List<T> findAll(Query query, Class<T> domainType) {
366
371
MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
367
372
String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource );
368
373
369
- return operations .query (sqlQuery , parameterSource , getEntityRowMapper (domainType ));
374
+ return operations .query (sqlQuery , parameterSource , getRowMapper (domainType ));
370
375
}
371
376
372
377
@ Override
@@ -375,7 +380,7 @@ public <T> List<T> findAll(Query query, Class<T> domainType, Pageable pageable)
375
380
MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
376
381
String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource , pageable );
377
382
378
- return operations .query (sqlQuery , parameterSource , getEntityRowMapper (domainType ));
383
+ return operations .query (sqlQuery , parameterSource , getRowMapper (domainType ));
379
384
}
380
385
381
386
@ Override
@@ -404,7 +409,13 @@ public <T> long count(Query query, Class<T> domainType) {
404
409
return result ;
405
410
}
406
411
407
- private <T > EntityRowMapper <T > getEntityRowMapper (Class <T > domainType ) {
412
+ private <T > RowMapper <? extends T > getRowMapper (Class <T > domainType ) {
413
+ RowMapper <? extends T > targetRowMapper ;
414
+
415
+ if ((targetRowMapper = queryMappingConfiguration .getRowMapper (domainType )) != null ) {
416
+ return targetRowMapper ;
417
+ }
418
+
408
419
return new EntityRowMapper <>(getRequiredPersistentEntity (domainType ), converter );
409
420
}
410
421
0 commit comments