Skip to content

Commit c38b3c4

Browse files
committed
Deprecate mutability of DelegatingDataAccessStrategy.
See: #1315 Original pull request: #1324.
1 parent 7475fd4 commit c38b3c4

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
4343

4444
private DataAccessStrategy delegate;
4545

46+
public DelegatingDataAccessStrategy() {}
47+
48+
public DelegatingDataAccessStrategy(DataAccessStrategy delegate) {
49+
50+
Assert.notNull(delegate, "DataAccessStrategy must not be null");
51+
this.delegate = delegate;
52+
}
53+
4654
@Override
4755
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) {
4856
return delegate.insert(instance, domainType, identifier, idValueSource);
@@ -182,7 +190,9 @@ public <T> long count(Query query, Class<T> domainType) {
182190
* Must be called exactly once before calling any of the other methods.
183191
*
184192
* @param delegate Must not be {@literal null}
193+
* @deprecated since 3.0, use {@link #DelegatingDataAccessStrategy(DataAccessStrategy)} to avoid mutable state.
185194
*/
195+
@Deprecated(since = "3.0", forRemoval = true)
186196
public void setDelegate(DataAccessStrategy delegate) {
187197

188198
Assert.isNull(this.delegate, "The delegate must be set exactly once");

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,6 @@ public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingC
8686
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlSession sqlSession,
8787
NamespaceStrategy namespaceStrategy, Dialect dialect) {
8888

89-
// the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency
90-
// cycle. In order to create it, we need something that allows to defer closing the cycle until all the elements are
91-
// created. That is the purpose of the DelegatingAccessStrategy.
92-
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
93-
MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy(sqlSession,
94-
dialect.getIdentifierProcessing());
95-
myBatisDataAccessStrategy.setNamespaceStrategy(namespaceStrategy);
96-
97-
CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy(
98-
asList(myBatisDataAccessStrategy, delegatingDataAccessStrategy));
9989

10090
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, converter, dialect);
10191
SqlParametersFactory sqlParametersFactory = new SqlParametersFactory(context, converter, dialect);
@@ -110,7 +100,17 @@ public static DataAccessStrategy createCombinedAccessStrategy(RelationalMappingC
110100
insertStrategyFactory //
111101
);
112102

113-
delegatingDataAccessStrategy.setDelegate(defaultDataAccessStrategy);
103+
// the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency
104+
// cycle. In order to create it, we need something that allows to defer closing the cycle until all the elements are
105+
// created. That is the purpose of the DelegatingAccessStrategy.
106+
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy(
107+
defaultDataAccessStrategy);
108+
MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy(sqlSession,
109+
dialect.getIdentifierProcessing());
110+
myBatisDataAccessStrategy.setNamespaceStrategy(namespaceStrategy);
111+
112+
CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy(
113+
asList(myBatisDataAccessStrategy, delegatingDataAccessStrategy));
114114

115115
return cascadingDataAccessStrategy;
116116
}
@@ -316,17 +316,17 @@ public <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable) {
316316
}
317317

318318
@Override
319-
public <T> Optional<T> selectOne(Query query, Class<T> probeType) {
319+
public <T> Optional<T> findOne(Query query, Class<T> probeType) {
320320
throw new UnsupportedOperationException("Not implemented");
321321
}
322322

323323
@Override
324-
public <T> Iterable<T> select(Query query, Class<T> probeType) {
324+
public <T> Iterable<T> findAll(Query query, Class<T> probeType) {
325325
throw new UnsupportedOperationException("Not implemented");
326326
}
327327

328328
@Override
329-
public <T> Iterable<T> select(Query query, Class<T> probeType, Pageable pageable) {
329+
public <T> Iterable<T> findAll(Query query, Class<T> probeType, Pageable pageable) {
330330
throw new UnsupportedOperationException("Not implemented");
331331
}
332332

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategyUnitTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@
4343
* @author Radim Tlusty
4444
* @author Chirag Tailor
4545
*/
46-
public class DefaultDataAccessStrategyUnitTests {
46+
class DefaultDataAccessStrategyUnitTests {
4747

48-
public static final long ORIGINAL_ID = 4711L;
48+
static final long ORIGINAL_ID = 4711L;
4949

50-
NamedParameterJdbcOperations namedJdbcOperations = mock(NamedParameterJdbcOperations.class);
51-
JdbcOperations jdbcOperations = mock(JdbcOperations.class);
52-
RelationalMappingContext context = new JdbcMappingContext();
53-
SqlParametersFactory sqlParametersFactory = mock(SqlParametersFactory.class);
54-
InsertStrategyFactory insertStrategyFactory = mock(InsertStrategyFactory.class);
50+
private NamedParameterJdbcOperations namedJdbcOperations = mock(NamedParameterJdbcOperations.class);
51+
private JdbcOperations jdbcOperations = mock(JdbcOperations.class);
52+
private RelationalMappingContext context = new JdbcMappingContext();
53+
private SqlParametersFactory sqlParametersFactory = mock(SqlParametersFactory.class);
54+
private InsertStrategyFactory insertStrategyFactory = mock(InsertStrategyFactory.class);
5555

56-
JdbcConverter converter;
57-
DefaultDataAccessStrategy accessStrategy;
56+
private JdbcConverter converter;
57+
private DefaultDataAccessStrategy accessStrategy;
5858

5959
@BeforeEach
60-
public void before() {
60+
void before() {
6161

6262
DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy();
6363
Dialect dialect = HsqlDbDialect.INSTANCE;
@@ -80,15 +80,15 @@ public void before() {
8080
}
8181

8282
@Test // GH-1159
83-
public void insert() {
83+
void insert() {
8484

8585
accessStrategy.insert(new DummyEntity(ORIGINAL_ID), DummyEntity.class, Identifier.empty(), IdValueSource.PROVIDED);
8686

8787
verify(insertStrategyFactory).insertStrategy(IdValueSource.PROVIDED, SqlIdentifier.quoted("ID"));
8888
}
8989

9090
@Test // GH-1159
91-
public void batchInsert() {
91+
void batchInsert() {
9292

9393
accessStrategy.insert(singletonList(InsertSubject.describedBy(new DummyEntity(ORIGINAL_ID), Identifier.empty())),
9494
DummyEntity.class, IdValueSource.PROVIDED);
@@ -97,7 +97,7 @@ public void batchInsert() {
9797
}
9898

9999
@Test // GH-1159
100-
public void insertForEntityWithNoId() {
100+
void insertForEntityWithNoId() {
101101

102102
accessStrategy.insert(new DummyEntityWithoutIdAnnotation(ORIGINAL_ID), DummyEntityWithoutIdAnnotation.class,
103103
Identifier.empty(), IdValueSource.GENERATED);
@@ -106,7 +106,7 @@ public void insertForEntityWithNoId() {
106106
}
107107

108108
@Test // GH-1159
109-
public void batchInsertForEntityWithNoId() {
109+
void batchInsertForEntityWithNoId() {
110110

111111
accessStrategy.insert(
112112
singletonList(InsertSubject.describedBy(new DummyEntityWithoutIdAnnotation(ORIGINAL_ID), Identifier.empty())),

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/SimpleJdbcRepositoryEventsUnitTests.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.Test;
3434
import org.mockito.stubbing.Answer;
35+
3536
import org.springframework.context.ApplicationEventPublisher;
3637
import org.springframework.data.annotation.Id;
3738
import org.springframework.data.domain.PageRequest;
3839
import org.springframework.data.domain.Pageable;
3940
import org.springframework.data.domain.Sort;
40-
import org.springframework.data.jdbc.core.convert.*;
41+
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
42+
import org.springframework.data.jdbc.core.convert.BatchJdbcOperations;
43+
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
44+
import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory;
45+
import org.springframework.data.jdbc.core.convert.DelegatingDataAccessStrategy;
46+
import org.springframework.data.jdbc.core.convert.InsertStrategyFactory;
47+
import org.springframework.data.jdbc.core.convert.JdbcConverter;
48+
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
49+
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
50+
import org.springframework.data.jdbc.core.convert.SqlParametersFactory;
4151
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4252
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
4353
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
@@ -73,22 +83,23 @@
7383
* @author Myeonghyeon Lee
7484
* @author Chirag Tailor
7585
*/
76-
public class SimpleJdbcRepositoryEventsUnitTests {
86+
class SimpleJdbcRepositoryEventsUnitTests {
7787

7888
private static final long generatedId = 4711L;
7989

80-
CollectingEventPublisher publisher = new CollectingEventPublisher();
90+
private CollectingEventPublisher publisher = new CollectingEventPublisher();
8191

82-
DummyEntityRepository repository;
83-
DefaultDataAccessStrategy dataAccessStrategy;
92+
private DummyEntityRepository repository;
93+
private DefaultDataAccessStrategy dataAccessStrategy;
8494

8595
@BeforeEach
86-
public void before() {
96+
void before() {
8797

8898
RelationalMappingContext context = new JdbcMappingContext();
8999
NamedParameterJdbcOperations operations = createIdGeneratingOperations();
90-
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
100+
91101
Dialect dialect = HsqlDbDialect.INSTANCE;
102+
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
92103
JdbcConverter converter = new BasicJdbcConverter(context, delegatingDataAccessStrategy, new JdbcCustomConversions(),
93104
new DefaultJdbcTypeFactory(operations.getJdbcOperations()), dialect.getIdentifierProcessing());
94105
SqlGeneratorSource generatorSource = new SqlGeneratorSource(context, converter, dialect);
@@ -109,7 +120,7 @@ public void before() {
109120

110121
@Test // DATAJDBC-99
111122
@SuppressWarnings("rawtypes")
112-
public void publishesEventsOnSave() {
123+
void publishesEventsOnSave() {
113124

114125
DummyEntity entity = new DummyEntity(23L);
115126

@@ -126,7 +137,7 @@ public void publishesEventsOnSave() {
126137

127138
@Test // DATAJDBC-99
128139
@SuppressWarnings("rawtypes")
129-
public void publishesEventsOnSaveMany() {
140+
void publishesEventsOnSaveMany() {
130141

131142
DummyEntity entity1 = new DummyEntity(null);
132143
DummyEntity entity2 = new DummyEntity(23L);
@@ -146,7 +157,7 @@ public void publishesEventsOnSaveMany() {
146157
}
147158

148159
@Test // DATAJDBC-99
149-
public void publishesEventsOnDelete() {
160+
void publishesEventsOnDelete() {
150161

151162
DummyEntity entity = new DummyEntity(23L);
152163

@@ -173,7 +184,7 @@ private Object getEntity(RelationalEvent e) {
173184

174185
@Test // DATAJDBC-99
175186
@SuppressWarnings("rawtypes")
176-
public void publishesEventsOnDeleteById() {
187+
void publishesEventsOnDeleteById() {
177188

178189
repository.deleteById(23L);
179190

@@ -187,7 +198,7 @@ public void publishesEventsOnDeleteById() {
187198

188199
@Test // DATAJDBC-197
189200
@SuppressWarnings("rawtypes")
190-
public void publishesEventsOnFindAll() {
201+
void publishesEventsOnFindAll() {
191202

192203
DummyEntity entity1 = new DummyEntity(42L);
193204
DummyEntity entity2 = new DummyEntity(23L);
@@ -206,7 +217,7 @@ public void publishesEventsOnFindAll() {
206217

207218
@Test // DATAJDBC-197
208219
@SuppressWarnings("rawtypes")
209-
public void publishesEventsOnFindAllById() {
220+
void publishesEventsOnFindAllById() {
210221

211222
DummyEntity entity1 = new DummyEntity(42L);
212223
DummyEntity entity2 = new DummyEntity(23L);
@@ -225,7 +236,7 @@ public void publishesEventsOnFindAllById() {
225236

226237
@Test // DATAJDBC-197
227238
@SuppressWarnings("rawtypes")
228-
public void publishesEventsOnFindById() {
239+
void publishesEventsOnFindById() {
229240

230241
DummyEntity entity1 = new DummyEntity(23L);
231242

@@ -242,7 +253,7 @@ public void publishesEventsOnFindById() {
242253

243254
@Test // DATAJDBC-101
244255
@SuppressWarnings("rawtypes")
245-
public void publishesEventsOnFindAllSorted() {
256+
void publishesEventsOnFindAllSorted() {
246257

247258
DummyEntity entity1 = new DummyEntity(42L);
248259
DummyEntity entity2 = new DummyEntity(23L);
@@ -261,7 +272,7 @@ public void publishesEventsOnFindAllSorted() {
261272

262273
@Test // DATAJDBC-101
263274
@SuppressWarnings("rawtypes")
264-
public void publishesEventsOnFindAllPaged() {
275+
void publishesEventsOnFindAllPaged() {
265276

266277
DummyEntity entity1 = new DummyEntity(42L);
267278
DummyEntity entity2 = new DummyEntity(23L);

0 commit comments

Comments
 (0)