Skip to content

Commit eab63d8

Browse files
committed
DATAJDBC-137 - Cleanups in the repository and repository.support packages.
1 parent fe98964 commit eab63d8

15 files changed

+243
-129
lines changed

src/main/java/org/springframework/data/jdbc/core/CascadingDataAccessStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
100100
}
101101

102102
private <T> T collect(Function<DataAccessStrategy, T> function) {
103-
return strategies.stream().collect(new FunctionCollector<>(function));
103+
return strategies.stream().collect(new FunctionCollector<T>(function));
104104
}
105105

106106
private void collectVoid(Consumer<DataAccessStrategy> consumer) {

src/main/java/org/springframework/data/jdbc/repository/RowMapperMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public interface RowMapperMap {
3030
*/
3131
RowMapperMap EMPTY = new RowMapperMap() {
3232

33+
/*
34+
* (non-Javadoc)
35+
* @see org.springframework.data.jdbc.repository.RowMapperMap#rowMapperFor(java.lang.Class)
36+
*/
3337
public <T> RowMapper<? extends T> rowMapperFor(Class<T> type) {
3438
return null;
3539
}

src/main/java/org/springframework/data/jdbc/repository/SimpleJdbcRepository.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,27 @@
1515
*/
1616
package org.springframework.data.jdbc.repository;
1717

18+
import lombok.NonNull;
19+
import lombok.RequiredArgsConstructor;
20+
1821
import java.util.ArrayList;
1922
import java.util.List;
2023
import java.util.Optional;
2124

2225
import org.springframework.data.jdbc.core.JdbcEntityOperations;
23-
import org.springframework.data.jdbc.core.JdbcEntityTemplate;
2426
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
2527
import org.springframework.data.repository.CrudRepository;
2628

2729
/**
2830
* @author Jens Schauder
31+
* @author Oliver Gierke
2932
* @since 1.0
3033
*/
34+
@RequiredArgsConstructor
3135
public class SimpleJdbcRepository<T, ID> implements CrudRepository<T, ID> {
3236

33-
private final JdbcPersistentEntityInformation<T, ID> entityInformation;
34-
35-
private final JdbcEntityOperations entityOperations;
36-
37-
/**
38-
* Creates a new {@link SimpleJdbcRepository}.
39-
*/
40-
public SimpleJdbcRepository(JdbcEntityTemplate entityOperations,
41-
JdbcPersistentEntityInformation<T, ID> entityInformation) {
42-
43-
this.entityOperations = entityOperations;
44-
this.entityInformation = entityInformation;
45-
}
37+
private final @NonNull JdbcEntityOperations entityOperations;
38+
private final @NonNull JdbcPersistentEntityInformation<T, ID> entityInformation;
4639

4740
/*
4841
* (non-Javadoc)
@@ -136,12 +129,9 @@ public void delete(T instance) {
136129
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
137130
*/
138131
@Override
132+
@SuppressWarnings("unchecked")
139133
public void deleteAll(Iterable<? extends T> entities) {
140-
141-
for (T entity : entities) {
142-
entityOperations.delete(entity, (Class<T>) entity.getClass());
143-
144-
}
134+
entities.forEach(it -> entityOperations.delete(it, (Class<T>) it.getClass()));
145135
}
146136

147137
@Override

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626
import org.springframework.data.repository.core.NamedQueries;
2727
import org.springframework.data.repository.core.RepositoryMetadata;
2828
import org.springframework.data.repository.query.QueryLookupStrategy;
29-
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
3029
import org.springframework.data.repository.query.RepositoryQuery;
3130
import org.springframework.jdbc.core.RowMapper;
3231
import org.springframework.jdbc.core.SingleColumnRowMapper;
32+
import org.springframework.util.Assert;
3333

3434
/**
3535
* {@link QueryLookupStrategy} for JDBC repositories. Currently only supports annotated queries.
3636
*
3737
* @author Jens Schauder
3838
* @author Kazuki Shimizu
39+
* @author Oliver Gierke
3940
* @since 1.0
4041
*/
4142
class JdbcQueryLookupStrategy implements QueryLookupStrategy {
@@ -45,15 +46,30 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
4546
private final RowMapperMap rowMapperMap;
4647
private final ConversionService conversionService;
4748

48-
JdbcQueryLookupStrategy(QueryMethodEvaluationContextProvider evaluationContextProvider, JdbcMappingContext context,
49-
DataAccessStrategy accessStrategy, RowMapperMap rowMapperMap) {
49+
/**
50+
* Creates a new {@link JdbcQueryLookupStrategy} for the given {@link JdbcMappingContext}, {@link DataAccessStrategy}
51+
* and {@link RowMapperMap}.
52+
*
53+
* @param context must not be {@literal null}.
54+
* @param accessStrategy must not be {@literal null}.
55+
* @param rowMapperMap must not be {@literal null}.
56+
*/
57+
JdbcQueryLookupStrategy(JdbcMappingContext context, DataAccessStrategy accessStrategy, RowMapperMap rowMapperMap) {
58+
59+
Assert.notNull(context, "JdbcMappingContext must not be null!");
60+
Assert.notNull(accessStrategy, "DataAccessStrategy must not be null!");
61+
Assert.notNull(rowMapperMap, "RowMapperMap must not be null!");
5062

5163
this.context = context;
5264
this.accessStrategy = accessStrategy;
5365
this.rowMapperMap = rowMapperMap;
5466
this.conversionService = context.getConversions();
5567
}
5668

69+
/*
70+
* (non-Javadoc)
71+
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
72+
*/
5773
@Override
5874
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
5975
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
@@ -78,7 +94,7 @@ private RowMapper<?> determineDefaultRowMapper(JdbcQueryMethod queryMethod) {
7894

7995
Class<?> domainType = queryMethod.getReturnedObjectType();
8096

81-
RowMapper typeMappedRowMapper = rowMapperMap.rowMapperFor(domainType);
97+
RowMapper<?> typeMappedRowMapper = rowMapperMap.rowMapperFor(domainType);
8298

8399
return typeMappedRowMapper == null //
84100
? new EntityRowMapper<>( //

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,73 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
4545
private final JdbcMappingContext context;
4646
private final ApplicationEventPublisher publisher;
4747
private final DataAccessStrategy accessStrategy;
48+
4849
private RowMapperMap rowMapperMap = RowMapperMap.EMPTY;
4950

50-
public JdbcRepositoryFactory(ApplicationEventPublisher publisher, JdbcMappingContext context,
51-
DataAccessStrategy dataAccessStrategy) {
51+
/**
52+
* Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy}, {@link JdbcMappingContext}
53+
* and {@link ApplicationEventPublisher}.
54+
*
55+
* @param dataAccessStrategy must not be {@literal null}.
56+
* @param context must not be {@literal null}.
57+
* @param publisher must not be {@literal null}.
58+
*/
59+
public JdbcRepositoryFactory(DataAccessStrategy dataAccessStrategy, JdbcMappingContext context,
60+
ApplicationEventPublisher publisher) {
61+
62+
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null!");
63+
Assert.notNull(context, "JdbcMappingContext must not be null!");
64+
Assert.notNull(publisher, "ApplicationEventPublisher must not be null!");
5265

5366
this.publisher = publisher;
5467
this.context = context;
5568
this.accessStrategy = dataAccessStrategy;
5669
}
5770

71+
/**
72+
* @param rowMapperMap must not be {@literal null} consider {@link RowMapperMap#EMPTY} instead.
73+
*/
74+
public void setRowMapperMap(RowMapperMap rowMapperMap) {
75+
76+
Assert.notNull(rowMapperMap, "RowMapperMap must not be null!");
77+
78+
this.rowMapperMap = rowMapperMap;
79+
}
80+
5881
@SuppressWarnings("unchecked")
5982
@Override
6083
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
6184
return (EntityInformation<T, ID>) context.getRequiredPersistentEntityInformation(aClass);
6285
}
6386

64-
@SuppressWarnings("unchecked")
87+
/*
88+
* (non-Javadoc)
89+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryInformation)
90+
*/
6591
@Override
6692
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
6793

68-
JdbcPersistentEntityInformation persistentEntityInformation = context
94+
JdbcPersistentEntityInformation<?, ?> persistentEntityInformation = context
6995
.getRequiredPersistentEntityInformation(repositoryInformation.getDomainType());
96+
7097
JdbcEntityTemplate template = new JdbcEntityTemplate(publisher, context, accessStrategy);
7198

7299
return new SimpleJdbcRepository<>(template, persistentEntityInformation);
73100
}
74101

102+
/*
103+
* (non-Javadoc)
104+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepositoryBaseClass(org.springframework.data.repository.core.RepositoryMetadata)
105+
*/
75106
@Override
76107
protected Class<?> getRepositoryBaseClass(RepositoryMetadata repositoryMetadata) {
77108
return SimpleJdbcRepository.class;
78109
}
79110

111+
/*
112+
* (non-Javadoc)
113+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
114+
*/
80115
@Override
81116
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrategy.Key key,
82117
QueryMethodEvaluationContextProvider evaluationContextProvider) {
@@ -88,15 +123,6 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrate
88123
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
89124
}
90125

91-
return Optional.of(new JdbcQueryLookupStrategy(evaluationContextProvider, context, accessStrategy, rowMapperMap));
92-
}
93-
94-
/**
95-
* @param rowMapperMap must not be {@literal null} consider {@link RowMapperMap#EMPTY} instead.
96-
*/
97-
public void setRowMapperMap(RowMapperMap rowMapperMap) {
98-
99-
Assert.notNull(rowMapperMap, "RowMapperMap must not be null!");
100-
this.rowMapperMap = rowMapperMap;
126+
return Optional.of(new JdbcQueryLookupStrategy(context, accessStrategy, rowMapperMap));
101127
}
102128
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Jens Schauder
3838
* @author Greg Turnquist
3939
* @author Christoph Strobl
40+
* @author Oliver Gierke
4041
* @since 1.0
4142
*/
4243
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> //
@@ -47,14 +48,24 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
4748
private DataAccessStrategy dataAccessStrategy;
4849
private RowMapperMap rowMapperMap = RowMapperMap.EMPTY;
4950

51+
/**
52+
* Creates a new {@link JdbcRepositoryFactoryBean} for the given repository interface.
53+
*
54+
* @param repositoryInterface must not be {@literal null}.
55+
*/
5056
JdbcRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
5157
super(repositoryInterface);
5258
}
5359

60+
/*
61+
* (non-Javadoc)
62+
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
63+
*/
5464
@Override
5565
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
5666

5767
super.setApplicationEventPublisher(publisher);
68+
5869
this.publisher = publisher;
5970
}
6071

@@ -66,8 +77,8 @@ public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
6677
@Override
6778
protected RepositoryFactorySupport doCreateRepositoryFactory() {
6879

69-
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(publisher, mappingContext,
70-
dataAccessStrategy);
80+
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(dataAccessStrategy, mappingContext,
81+
publisher);
7182
jdbcRepositoryFactory.setRowMapperMap(rowMapperMap);
7283

7384
return jdbcRepositoryFactory;
@@ -97,20 +108,23 @@ public void setRowMapperMap(RowMapperMap rowMapperMap) {
97108
this.rowMapperMap = rowMapperMap;
98109
}
99110

111+
/*
112+
* (non-Javadoc)
113+
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#afterPropertiesSet()
114+
*/
100115
@Override
101116
public void afterPropertiesSet() {
102117

103118
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null!");
104119

105120
if (dataAccessStrategy == null) {
106121

107-
dataAccessStrategy = new DefaultDataAccessStrategy( //
108-
new SqlGeneratorSource(mappingContext), //
109-
mappingContext);
122+
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(mappingContext);
123+
this.dataAccessStrategy = new DefaultDataAccessStrategy(sqlGeneratorSource, mappingContext);
110124
}
111125

112126
if (rowMapperMap == null) {
113-
rowMapperMap = RowMapperMap.EMPTY;
127+
this.rowMapperMap = RowMapperMap.EMPTY;
114128
}
115129

116130
super.afterPropertiesSet();

0 commit comments

Comments
 (0)