Skip to content

Commit 42e51a0

Browse files
committed
GH-1006 included QueryMappingConfiguration for all part tree queries
Signed-off-by: mipo256 <[email protected]>
1 parent f3dc789 commit 42e51a0

29 files changed

+231
-44
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DataAccessStrategyFactory.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import java.util.Optional;
19+
1820
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
21+
import org.springframework.lang.Nullable;
1922
import org.springframework.util.Assert;
2023

2124
/**
@@ -25,6 +28,7 @@
2528
* {@link DataAccessStrategy} for consistent access strategy creation.
2629
*
2730
* @author Mark Paluch
31+
* @author Mikhail Polivakha
2832
* @since 3.2
2933
*/
3034
public class DataAccessStrategyFactory {
@@ -34,6 +38,7 @@ public class DataAccessStrategyFactory {
3438
private final NamedParameterJdbcOperations operations;
3539
private final SqlParametersFactory sqlParametersFactory;
3640
private final InsertStrategyFactory insertStrategyFactory;
41+
private final QueryMappingConfiguration queryMappingConfiguration;
3742

3843
/**
3944
* Creates a new {@link DataAccessStrategyFactory}.
@@ -43,22 +48,25 @@ public class DataAccessStrategyFactory {
4348
* @param operations must not be {@literal null}.
4449
* @param sqlParametersFactory must not be {@literal null}.
4550
* @param insertStrategyFactory must not be {@literal null}.
51+
* @param queryMappingConfiguration must not be {@literal null}.
4652
*/
4753
public DataAccessStrategyFactory(SqlGeneratorSource sqlGeneratorSource, JdbcConverter converter,
4854
NamedParameterJdbcOperations operations, SqlParametersFactory sqlParametersFactory,
49-
InsertStrategyFactory insertStrategyFactory) {
55+
InsertStrategyFactory insertStrategyFactory, QueryMappingConfiguration queryMappingConfiguration) {
5056

5157
Assert.notNull(sqlGeneratorSource, "SqlGeneratorSource must not be null");
5258
Assert.notNull(converter, "JdbcConverter must not be null");
5359
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
5460
Assert.notNull(sqlParametersFactory, "SqlParametersFactory must not be null");
5561
Assert.notNull(insertStrategyFactory, "InsertStrategyFactory must not be null");
62+
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
5663

5764
this.sqlGeneratorSource = sqlGeneratorSource;
5865
this.converter = converter;
5966
this.operations = operations;
6067
this.sqlParametersFactory = sqlParametersFactory;
6168
this.insertStrategyFactory = insertStrategyFactory;
69+
this.queryMappingConfiguration = queryMappingConfiguration;
6270
}
6371

6472
/**
@@ -70,7 +78,7 @@ public DataAccessStrategy create() {
7078

7179
DefaultDataAccessStrategy defaultDataAccessStrategy = new DefaultDataAccessStrategy(sqlGeneratorSource,
7280
this.converter.getMappingContext(), this.converter, this.operations, sqlParametersFactory,
73-
insertStrategyFactory);
81+
insertStrategyFactory, queryMappingConfiguration);
7482

7583
if (this.converter.getMappingContext().isSingleQueryLoadingEnabled()) {
7684
return new SingleQueryFallbackDataAccessStrategy(sqlGeneratorSource, converter, operations,

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

+25-14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Chirag Tailor
6363
* @author Diego Krupitza
6464
* @author Sergey Korotaev
65+
* @author Mikhail Polivakha
6566
* @since 1.1
6667
*/
6768
public 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

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/QueryMappingConfiguration.java spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMappingConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.springframework.data.jdbc.repository;
1+
package org.springframework.data.jdbc.core.convert;
22

33
import org.springframework.jdbc.core.RowMapper;
44
import org.springframework.lang.Nullable;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.data.domain.Pageable;
3333
import org.springframework.data.domain.Sort;
3434
import org.springframework.data.jdbc.core.convert.*;
35+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
3536
import org.springframework.data.mapping.PersistentPropertyPath;
3637
import org.springframework.data.mapping.PropertyPath;
3738
import org.springframework.data.relational.core.conversion.IdValueSource;
@@ -76,9 +77,10 @@ public class MyBatisDataAccessStrategy implements DataAccessStrategy {
7677
* uses a {@link DefaultDataAccessStrategy}
7778
*/
7879
public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingContext context,
79-
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlSession sqlSession, Dialect dialect) {
80+
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlSession sqlSession,
81+
Dialect dialect, QueryMappingConfiguration queryMappingConfiguration) {
8082
return createCombinedAccessStrategy(context, converter, operations, sqlSession, NamespaceStrategy.DEFAULT_INSTANCE,
81-
dialect);
83+
dialect, queryMappingConfiguration);
8284
}
8385

8486
/**
@@ -87,7 +89,7 @@ public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingC
8789
*/
8890
public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingContext context,
8991
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlSession sqlSession,
90-
NamespaceStrategy namespaceStrategy, Dialect dialect) {
92+
NamespaceStrategy namespaceStrategy, Dialect dialect, QueryMappingConfiguration queryMappingConfiguration) {
9193

9294
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, converter, dialect);
9395
SqlParametersFactory sqlParametersFactory = new SqlParametersFactory(context, converter);
@@ -98,7 +100,8 @@ public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingC
98100
converter, //
99101
operations, //
100102
sqlParametersFactory, //
101-
insertStrategyFactory //
103+
insertStrategyFactory, //
104+
queryMappingConfiguration //
102105
).create();
103106

104107
// the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/AbstractJdbcConfiguration.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.data.jdbc.core.mapping.IdGeneratingBeforeSaveCallback;
4242
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4343
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
44+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
4445
import org.springframework.data.mapping.model.SimpleTypeHolder;
4546
import org.springframework.data.relational.RelationalManagedTypes;
4647
import org.springframework.data.relational.core.conversion.RelationalConverter;
@@ -62,6 +63,7 @@
6263
* @author Christoph Strobl
6364
* @author Myeonghyeon Lee
6465
* @author Chirag Tailor
66+
* @author Mikhail Polivakha
6567
* @since 1.1
6668
*/
6769
@Configuration(proxyBeanMethods = false)
@@ -71,6 +73,8 @@ public class AbstractJdbcConfiguration implements ApplicationContextAware {
7173

7274
private ApplicationContext applicationContext;
7375

76+
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
77+
7478
/**
7579
* Returns the base packages to scan for JDBC mapped entities at startup. Returns the package name of the
7680
* configuration class' (the concrete class, not this one here) by default. So if you have a
@@ -225,7 +229,9 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
225229
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, jdbcConverter, dialect);
226230
DataAccessStrategyFactory factory = new DataAccessStrategyFactory(sqlGeneratorSource, jdbcConverter, operations,
227231
new SqlParametersFactory(context, jdbcConverter),
228-
new InsertStrategyFactory(operations, dialect));
232+
new InsertStrategyFactory(operations, dialect),
233+
this.queryMappingConfiguration
234+
);
229235

230236
return factory.create();
231237
}
@@ -249,6 +255,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
249255
this.applicationContext = applicationContext;
250256
}
251257

258+
public void setQueryMappingConfiguration(Optional<QueryMappingConfiguration> queryMappingConfiguration) throws BeansException {
259+
this.queryMappingConfiguration = queryMappingConfiguration.orElse(QueryMappingConfiguration.EMPTY);
260+
}
261+
252262
/**
253263
* Scans the mapping base package for classes annotated with {@link Table}. By default, it scans for entities in all
254264
* packages returned by {@link #getMappingBasePackages()}.

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DefaultQueryMappingConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedHashMap;
44
import java.util.Map;
55

6-
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
6+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
77
import org.springframework.jdbc.core.ResultSetExtractor;
88
import org.springframework.jdbc.core.RowMapper;
99
import org.springframework.lang.Nullable;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/MyBatisJdbcConfiguration.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.config;
1717

18+
import java.util.Optional;
19+
1820
import org.apache.ibatis.session.SqlSession;
1921
import org.springframework.beans.factory.annotation.Autowired;
2022
import org.springframework.context.annotation.Bean;
@@ -23,25 +25,29 @@
2325
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2426
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
2527
import org.springframework.data.jdbc.mybatis.MyBatisDataAccessStrategy;
28+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
2629
import org.springframework.data.relational.core.dialect.Dialect;
2730
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
2831

2932
/**
3033
* Configuration class tweaking Spring Data JDBC to use a {@link MyBatisDataAccessStrategy} instead of the default one.
3134
*
3235
* @author Oliver Drotbohm
36+
* @author Mikhail Polivakha
3337
* @since 1.1
3438
*/
3539
@Configuration(proxyBeanMethods = false)
3640
public class MyBatisJdbcConfiguration extends AbstractJdbcConfiguration {
3741

3842
private @Autowired SqlSession session;
3943

44+
private @Autowired Optional<QueryMappingConfiguration> queryMappingConfiguration;
45+
4046
@Bean
4147
@Override
4248
public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations operations, JdbcConverter jdbcConverter,
4349
JdbcMappingContext context, Dialect dialect) {
4450

45-
return MyBatisDataAccessStrategy.createCombinedAccessStrategy(context, jdbcConverter, operations, session, dialect);
51+
return MyBatisDataAccessStrategy.createCombinedAccessStrategy(context, jdbcConverter, operations, session, dialect, queryMappingConfiguration.orElse(QueryMappingConfiguration.EMPTY));
4652
}
4753
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.context.ApplicationEventPublisher;
2626
import org.springframework.data.jdbc.core.convert.EntityRowMapper;
2727
import org.springframework.data.jdbc.core.convert.JdbcConverter;
28-
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
28+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
2929
import org.springframework.data.jdbc.repository.query.AbstractJdbcQuery;
3030
import org.springframework.data.jdbc.repository.query.JdbcQueryMethod;
3131
import org.springframework.data.jdbc.repository.query.PartTreeJdbcQuery;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
2323
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2424
import org.springframework.data.jdbc.core.convert.JdbcConverter;
25-
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
25+
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
2626
import org.springframework.data.mapping.callback.EntityCallbacks;
2727
import org.springframework.data.relational.core.dialect.Dialect;
2828
import org.springframework.data.relational.core.mapping.RelationalMappingContext;

0 commit comments

Comments
 (0)