6262 * @author Chirag Tailor
6363 * @author Diego Krupitza
6464 * @author Sergey Korotaev
65+ * @author Mikhail Polivakha
6566 * @since 1.1
6667 */
6768public class DefaultDataAccessStrategy implements DataAccessStrategy {
@@ -73,6 +74,8 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
7374 private final SqlParametersFactory sqlParametersFactory ;
7475 private final InsertStrategyFactory insertStrategyFactory ;
7576
77+ private final QueryMappingConfiguration queryMappingConfiguration ;
78+
7679 /**
7780 * Creates a {@link DefaultDataAccessStrategy}
7881 *
@@ -84,21 +87,23 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
8487 */
8588 public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , RelationalMappingContext context ,
8689 JdbcConverter converter , NamedParameterJdbcOperations operations , SqlParametersFactory sqlParametersFactory ,
87- InsertStrategyFactory insertStrategyFactory ) {
90+ InsertStrategyFactory insertStrategyFactory , QueryMappingConfiguration queryMappingConfiguration ) {
8891
8992 Assert .notNull (sqlGeneratorSource , "SqlGeneratorSource must not be null" );
9093 Assert .notNull (context , "RelationalMappingContext must not be null" );
9194 Assert .notNull (converter , "JdbcConverter must not be null" );
9295 Assert .notNull (operations , "NamedParameterJdbcOperations must not be null" );
9396 Assert .notNull (sqlParametersFactory , "SqlParametersFactory must not be null" );
9497 Assert .notNull (insertStrategyFactory , "InsertStrategyFactory must not be null" );
98+ Assert .notNull (queryMappingConfiguration , "QueryMappingConfiguration must not be null" );
9599
96100 this .sqlGeneratorSource = sqlGeneratorSource ;
97101 this .context = context ;
98102 this .converter = converter ;
99103 this .operations = operations ;
100104 this .sqlParametersFactory = sqlParametersFactory ;
101105 this .insertStrategyFactory = insertStrategyFactory ;
106+ this .queryMappingConfiguration = queryMappingConfiguration ;
102107 }
103108
104109 @ Override
@@ -272,21 +277,21 @@ public <T> T findById(Object id, Class<T> domainType) {
272277 SqlIdentifierParameterSource parameter = sqlParametersFactory .forQueryById (id , domainType , ID_SQL_PARAMETER );
273278
274279 try {
275- return operations .queryForObject (findOneSql , parameter , getEntityRowMapper (domainType ));
280+ return operations .queryForObject (findOneSql , parameter , getRowMapper (domainType ));
276281 } catch (EmptyResultDataAccessException e ) {
277282 return null ;
278283 }
279284 }
280285
281286 @ Override
282287 public <T > List <T > findAll (Class <T > domainType ) {
283- return operations .query (sql (domainType ).getFindAll (), getEntityRowMapper (domainType ));
288+ return operations .query (sql (domainType ).getFindAll (), getRowMapper (domainType ));
284289 }
285290
286291 @ Override
287292 public <T > Stream <T > streamAll (Class <T > domainType ) {
288293 return operations .queryForStream (sql (domainType ).getFindAll (), new MapSqlParameterSource (),
289- getEntityRowMapper (domainType ));
294+ getRowMapper (domainType ));
290295 }
291296
292297 @ Override
@@ -298,7 +303,7 @@ public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {
298303
299304 SqlParameterSource parameterSource = sqlParametersFactory .forQueryByIds (ids , domainType );
300305 String findAllInListSql = sql (domainType ).getFindAllInList ();
301- return operations .query (findAllInListSql , parameterSource , getEntityRowMapper (domainType ));
306+ return operations .query (findAllInListSql , parameterSource , getRowMapper (domainType ));
302307 }
303308
304309 @ Override
@@ -311,7 +316,7 @@ public <T> Stream<T> streamAllByIds(Iterable<?> ids, Class<T> domainType) {
311316 SqlParameterSource parameterSource = sqlParametersFactory .forQueryByIds (ids , domainType );
312317 String findAllInListSql = sql (domainType ).getFindAllInList ();
313318
314- return operations .queryForStream (findAllInListSql , parameterSource , getEntityRowMapper (domainType ));
319+ return operations .queryForStream (findAllInListSql , parameterSource , getRowMapper (domainType ));
315320 }
316321
317322 @ Override
@@ -365,18 +370,18 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
365370
366371 @ Override
367372 public <T > List <T > findAll (Class <T > domainType , Sort sort ) {
368- return operations .query (sql (domainType ).getFindAll (sort ), getEntityRowMapper (domainType ));
373+ return operations .query (sql (domainType ).getFindAll (sort ), getRowMapper (domainType ));
369374 }
370375
371376 @ Override
372377 public <T > Stream <T > streamAll (Class <T > domainType , Sort sort ) {
373378 return operations .queryForStream (sql (domainType ).getFindAll (sort ), new MapSqlParameterSource (),
374- getEntityRowMapper (domainType ));
379+ getRowMapper (domainType ));
375380 }
376381
377382 @ Override
378383 public <T > List <T > findAll (Class <T > domainType , Pageable pageable ) {
379- return operations .query (sql (domainType ).getFindAll (pageable ), getEntityRowMapper (domainType ));
384+ return operations .query (sql (domainType ).getFindAll (pageable ), getRowMapper (domainType ));
380385 }
381386
382387 @ Override
@@ -386,7 +391,7 @@ public <T> Optional<T> findOne(Query query, Class<T> domainType) {
386391 String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource );
387392
388393 try {
389- return Optional .ofNullable (operations .queryForObject (sqlQuery , parameterSource , getEntityRowMapper (domainType )));
394+ return Optional .ofNullable (operations .queryForObject (sqlQuery , parameterSource , getRowMapper (domainType )));
390395 } catch (EmptyResultDataAccessException e ) {
391396 return Optional .empty ();
392397 }
@@ -398,7 +403,7 @@ public <T> List<T> findAll(Query query, Class<T> domainType) {
398403 MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
399404 String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource );
400405
401- return operations .query (sqlQuery , parameterSource , getEntityRowMapper (domainType ));
406+ return operations .query (sqlQuery , parameterSource , getRowMapper (domainType ));
402407 }
403408
404409 @ Override
@@ -407,7 +412,7 @@ public <T> Stream<T> streamAll(Query query, Class<T> domainType) {
407412 MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
408413 String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource );
409414
410- return operations .queryForStream (sqlQuery , parameterSource , getEntityRowMapper (domainType ));
415+ return operations .queryForStream (sqlQuery , parameterSource , getRowMapper (domainType ));
411416 }
412417
413418 @ Override
@@ -416,7 +421,7 @@ public <T> List<T> findAll(Query query, Class<T> domainType, Pageable pageable)
416421 MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
417422 String sqlQuery = sql (domainType ).selectByQuery (query , parameterSource , pageable );
418423
419- return operations .query (sqlQuery , parameterSource , getEntityRowMapper (domainType ));
424+ return operations .query (sqlQuery , parameterSource , getRowMapper (domainType ));
420425 }
421426
422427 @ Override
@@ -445,7 +450,13 @@ public <T> long count(Query query, Class<T> domainType) {
445450 return result ;
446451 }
447452
448- private <T > EntityRowMapper <T > getEntityRowMapper (Class <T > domainType ) {
453+ private <T > RowMapper <? extends T > getRowMapper (Class <T > domainType ) {
454+ RowMapper <? extends T > targetRowMapper ;
455+
456+ if ((targetRowMapper = queryMappingConfiguration .getRowMapper (domainType )) != null ) {
457+ return targetRowMapper ;
458+ }
459+
449460 return new EntityRowMapper <>(getRequiredPersistentEntity (domainType ), converter );
450461 }
451462
0 commit comments