Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 932d9ce

Browse files
committedJan 5, 2024
add jdbcAggregateOperationsRef to @EnableJdbcRepositories
1 parent 5647835 commit 932d9ce

16 files changed

+181
-110
lines changed
 

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.data.domain.Page;
2323
import org.springframework.data.domain.Pageable;
2424
import org.springframework.data.domain.Sort;
25+
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
26+
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2527
import org.springframework.data.relational.core.query.Query;
2628
import org.springframework.lang.Nullable;
2729

@@ -34,6 +36,7 @@
3436
* @author Chirag Tailor
3537
* @author Diego Krupitza
3638
* @author Myeonghyeon Lee
39+
* @author Tomohiko Ozawa
3740
*/
3841
public interface JdbcAggregateOperations {
3942

@@ -312,4 +315,18 @@ default <T> void delete(T aggregateRoot, Class<T> domainType) {
312315
default <T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType) {
313316
deleteAll(aggregateRoots);
314317
}
318+
319+
/**
320+
* Returns the {@link JdbcConverter}.
321+
*
322+
* @return the {@link JdbcConverter}.
323+
*/
324+
JdbcConverter getConverter();
325+
326+
/**
327+
* Return the {@link DataAccessStrategy}
328+
*
329+
* @return the {@link DataAccessStrategy}
330+
*/
331+
DataAccessStrategy getDataAccessStrategy();
315332
}

‎spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* @author Myeonghyeon Lee
6868
* @author Chirag Tailor
6969
* @author Diego Krupitza
70+
* @author Tomohiko Ozawa
7071
*/
7172
public class JdbcAggregateTemplate implements JdbcAggregateOperations {
7273

@@ -86,22 +87,20 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
8687
* {@link DataAccessStrategy}.
8788
*
8889
* @param publisher must not be {@literal null}.
89-
* @param context must not be {@literal null}.
9090
* @param dataAccessStrategy must not be {@literal null}.
9191
* @since 1.1
9292
*/
93-
public JdbcAggregateTemplate(ApplicationContext publisher, RelationalMappingContext context, JdbcConverter converter,
93+
public JdbcAggregateTemplate(ApplicationContext publisher, JdbcConverter converter,
9494
DataAccessStrategy dataAccessStrategy) {
9595

9696
Assert.notNull(publisher, "ApplicationContext must not be null");
97-
Assert.notNull(context, "RelationalMappingContext must not be null");
9897
Assert.notNull(converter, "RelationalConverter must not be null");
9998
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
10099

101100
this.eventDelegate.setPublisher(publisher);
102-
this.context = context;
103-
this.accessStrategy = dataAccessStrategy;
104101
this.converter = converter;
102+
this.accessStrategy = dataAccessStrategy;
103+
this.context = converter.getMappingContext();
105104

106105
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
107106

@@ -115,21 +114,19 @@ public JdbcAggregateTemplate(ApplicationContext publisher, RelationalMappingCont
115114
* {@link RelationalMappingContext} and {@link DataAccessStrategy}.
116115
*
117116
* @param publisher must not be {@literal null}.
118-
* @param context must not be {@literal null}.
119117
* @param dataAccessStrategy must not be {@literal null}.
120118
*/
121-
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, RelationalMappingContext context,
122-
JdbcConverter converter, DataAccessStrategy dataAccessStrategy) {
119+
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, JdbcConverter converter,
120+
DataAccessStrategy dataAccessStrategy) {
123121

124122
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
125-
Assert.notNull(context, "RelationalMappingContext must not be null");
126123
Assert.notNull(converter, "RelationalConverter must not be null");
127124
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
128125

129126
this.eventDelegate.setPublisher(publisher);
130-
this.context = context;
131-
this.accessStrategy = dataAccessStrategy;
132127
this.converter = converter;
128+
this.accessStrategy = dataAccessStrategy;
129+
this.context = converter.getMappingContext();
133130

134131
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
135132
this.executor = new AggregateChangeExecutor(converter, accessStrategy);
@@ -656,9 +653,19 @@ private <T> T triggerBeforeDelete(@Nullable T aggregateRoot, Object id, MutableA
656653
return null;
657654
}
658655

659-
private record EntityAndPreviousVersion<T> (T entity, @Nullable Number version) {
656+
private record EntityAndPreviousVersion<T>(T entity, @Nullable Number version) {
660657
}
661658

662-
private record EntityAndChangeCreator<T> (T entity, Function<T, RootAggregateChange<T>> changeCreator) {
659+
private record EntityAndChangeCreator<T>(T entity, Function<T, RootAggregateChange<T>> changeCreator) {
660+
}
661+
662+
@Override
663+
public DataAccessStrategy getDataAccessStrategy() {
664+
return accessStrategy;
665+
}
666+
667+
@Override
668+
public JdbcConverter getConverter() {
669+
return converter;
663670
}
664671
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jdbc.repository.config;
16+
package org.springframework.data.jdbc.dialect;
1717

1818
import java.sql.Connection;
1919
import java.sql.DatabaseMetaData;

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

+16-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828
import org.springframework.beans.BeansException;
29-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3029
import org.springframework.context.ApplicationContext;
3130
import org.springframework.context.ApplicationContextAware;
3231
import org.springframework.context.annotation.Bean;
@@ -40,6 +39,7 @@
4039
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
4140
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4241
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
42+
import org.springframework.data.jdbc.dialect.DialectResolver;
4343
import org.springframework.data.mapping.model.SimpleTypeHolder;
4444
import org.springframework.data.relational.RelationalManagedTypes;
4545
import org.springframework.data.relational.core.conversion.RelationalConverter;
@@ -61,6 +61,7 @@
6161
* @author Christoph Strobl
6262
* @author Myeonghyeon Lee
6363
* @author Chirag Tailor
64+
* @author Tomohiko Ozawa
6465
* @since 1.1
6566
*/
6667
@Configuration(proxyBeanMethods = false)
@@ -103,7 +104,7 @@ public RelationalManagedTypes jdbcManagedTypes() throws ClassNotFoundException {
103104
*
104105
* @param namingStrategy optional {@link NamingStrategy}. Use
105106
* {@link org.springframework.data.relational.core.mapping.DefaultNamingStrategy#INSTANCE} as fallback.
106-
* @param customConversions see {@link #jdbcCustomConversions()}.
107+
* @param customConversions see {@link #jdbcCustomConversions(Optional)}.
107108
* @param jdbcManagedTypes JDBC managed types, typically discovered through {@link #jdbcManagedTypes() an entity
108109
* scan}.
109110
* @return must not be {@literal null}.
@@ -124,7 +125,7 @@ public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStra
124125
* {@link #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)}.
125126
*
126127
* @see #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)
127-
* @see #jdbcCustomConversions()
128+
* @see #jdbcCustomConversions(Optional)
128129
* @return must not be {@literal null}.
129130
*/
130131
@Bean
@@ -147,23 +148,17 @@ public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, NamedParam
147148
* @return will never be {@literal null}.
148149
*/
149150
@Bean
150-
public JdbcCustomConversions jdbcCustomConversions() {
151-
152-
try {
153-
154-
Dialect dialect = applicationContext.getBean(Dialect.class);
155-
SimpleTypeHolder simpleTypeHolder = dialect.simpleTypes().isEmpty() ? JdbcSimpleTypes.HOLDER
156-
: new SimpleTypeHolder(dialect.simpleTypes(), JdbcSimpleTypes.HOLDER);
157-
158-
return new JdbcCustomConversions(
159-
CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(dialect)), userConverters());
160-
161-
} catch (NoSuchBeanDefinitionException exception) {
162-
151+
public JdbcCustomConversions jdbcCustomConversions(Optional<Dialect> dialect) {
152+
return dialect.map(d -> {
153+
SimpleTypeHolder simpleTypeHolder = d.simpleTypes().isEmpty()
154+
? JdbcSimpleTypes.HOLDER
155+
: new SimpleTypeHolder(d.simpleTypes(), JdbcSimpleTypes.HOLDER);
156+
return new JdbcCustomConversions(CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(d)),
157+
userConverters());
158+
}).orElseGet(() -> {
163159
LOG.warn("No dialect found; CustomConversions will be configured without dialect specific conversions");
164-
165160
return new JdbcCustomConversions();
166-
}
161+
});
167162
}
168163

169164
protected List<?> userConverters() {
@@ -191,7 +186,7 @@ private List<Object> storeConverters(Dialect dialect) {
191186
public JdbcAggregateTemplate jdbcAggregateTemplate(ApplicationContext applicationContext,
192187
JdbcMappingContext mappingContext, JdbcConverter converter, DataAccessStrategy dataAccessStrategy) {
193188

194-
return new JdbcAggregateTemplate(applicationContext, mappingContext, converter, dataAccessStrategy);
189+
return new JdbcAggregateTemplate(applicationContext, converter, dataAccessStrategy);
195190
}
196191

197192
/**
@@ -207,8 +202,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
207202

208203
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, jdbcConverter, dialect);
209204
DataAccessStrategyFactory factory = new DataAccessStrategyFactory(sqlGeneratorSource, jdbcConverter, operations,
210-
new SqlParametersFactory(context, jdbcConverter),
211-
new InsertStrategyFactory(operations, dialect));
205+
new SqlParametersFactory(context, jdbcConverter), new InsertStrategyFactory(operations, dialect));
212206

213207
return factory.create();
214208
}
@@ -219,8 +213,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
219213
* @param operations the {@link NamedParameterJdbcOperations} allowing access to a {@link java.sql.Connection}.
220214
* @return the {@link Dialect} to be used.
221215
* @since 2.0
222-
* @throws org.springframework.data.jdbc.repository.config.DialectResolver.NoDialectException if the {@link Dialect}
223-
* cannot be determined.
216+
* @throws DialectResolver.NoDialectException if the {@link Dialect} cannot be determined.
224217
*/
225218
@Bean
226219
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@
122122
/**
123123
* Configures the name of the {@link org.springframework.data.jdbc.core.convert.DataAccessStrategy} bean definition to
124124
* be used to create repositories discovered through this annotation. Defaults to {@code defaultDataAccessStrategy}.
125+
* @deprecated since 3.3 use {@link #jdbcAggregateOperationsRef()} instead
125126
*/
127+
@Deprecated(since = "3.3")
126128
String dataAccessStrategyRef() default "";
127129

128130
/**
@@ -133,6 +135,12 @@
133135
*/
134136
String transactionManagerRef() default "transactionManager";
135137

138+
/**
139+
* Configure the name of the {@link org.springframework.data.jdbc.core.JdbcAggregateOperations} bean definition to be
140+
* used to create repositories discovered through this annotation.
141+
*/
142+
String jdbcAggregateOperationsRef() default "";
143+
136144
/**
137145
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
138146
* {@link QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}.

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Fei Dong
4141
* @author Mark Paluch
4242
* @author Antoine Sauray
43+
* @author Tomohiko Ozawa
4344
*/
4445
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
4546

@@ -79,9 +80,15 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
7980
Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
8081
builder.addPropertyValue("transactionManager", transactionManagerRef.orElse(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME));
8182

82-
builder.addPropertyValue("mappingContext", new RuntimeBeanReference(JdbcMappingContext.class));
83-
builder.addPropertyValue("dialect", new RuntimeBeanReference(Dialect.class));
84-
builder.addPropertyValue("converter", new RuntimeBeanReference(JdbcConverter.class));
83+
Optional<String> jdbcAggregateOperationsRef = source.getAttribute("jdbcAggregateOperationsRef");
84+
85+
if (jdbcAggregateOperationsRef.isPresent()) {
86+
builder.addPropertyReference("jdbcAggregateOperations", jdbcAggregateOperationsRef.get());
87+
} else {
88+
builder.addPropertyValue("mappingContext", new RuntimeBeanReference(JdbcMappingContext.class));
89+
builder.addPropertyValue("dialect", new RuntimeBeanReference(Dialect.class));
90+
builder.addPropertyValue("converter", new RuntimeBeanReference(JdbcConverter.class));
91+
}
8592
}
8693

8794
/**

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import org.springframework.beans.factory.BeanFactory;
2121
import org.springframework.context.ApplicationEventPublisher;
22+
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
2223
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
2324
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2425
import org.springframework.data.jdbc.core.convert.JdbcConverter;
26+
import org.springframework.data.jdbc.dialect.DialectResolver;
2527
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
2628
import org.springframework.data.mapping.callback.EntityCallbacks;
2729
import org.springframework.data.relational.core.dialect.Dialect;
@@ -48,6 +50,7 @@
4850
* @author Hebert Coelho
4951
* @author Diego Krupitza
5052
* @author Christopher Klein
53+
* @author Tomohiko Ozawa
5154
*/
5255
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5356

@@ -62,6 +65,20 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
6265
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6366
private EntityCallbacks entityCallbacks;
6467

68+
public JdbcRepositoryFactory(ApplicationEventPublisher publisher, JdbcAggregateOperations jdbcAggregateOperations,
69+
NamedParameterJdbcOperations operations) {
70+
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
71+
Assert.notNull(jdbcAggregateOperations, "JdbcAggregateOperations must not be null");
72+
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
73+
74+
this.converter = jdbcAggregateOperations.getConverter();
75+
this.accessStrategy = jdbcAggregateOperations.getDataAccessStrategy();
76+
this.context = jdbcAggregateOperations.getConverter().getMappingContext();
77+
this.dialect = DialectResolver.getDialect(operations.getJdbcOperations());
78+
this.operations = operations;
79+
this.publisher = publisher;
80+
}
81+
6582
/**
6683
* Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy},
6784
* {@link RelationalMappingContext} and {@link ApplicationEventPublisher}.
@@ -114,7 +131,7 @@ public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
114131
@Override
115132
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
116133

117-
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
134+
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, converter, accessStrategy);
118135

119136
if (entityCallbacks != null) {
120137
template.setEntityCallbacks(entityCallbacks);

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

+30-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.context.ApplicationEventPublisher;
2323
import org.springframework.context.ApplicationEventPublisherAware;
24+
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
25+
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
2426
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2527
import org.springframework.data.jdbc.core.convert.DataAccessStrategyFactory;
2628
import org.springframework.data.jdbc.core.convert.InsertStrategyFactory;
@@ -48,6 +50,7 @@
4850
* @author Mark Paluch
4951
* @author Hebert Coelho
5052
* @author Chirag Tailor
53+
* @author Tomohiko Ozawa
5154
*/
5255
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
5356
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
@@ -59,6 +62,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
5962
private DataAccessStrategy dataAccessStrategy;
6063
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6164
private NamedParameterJdbcOperations operations;
65+
private JdbcAggregateOperations aggregateOperations;
6266
private EntityCallbacks entityCallbacks;
6367
private Dialect dialect;
6468

@@ -85,8 +89,7 @@ public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
8589
@Override
8690
protected RepositoryFactorySupport doCreateRepositoryFactory() {
8791

88-
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(dataAccessStrategy, mappingContext,
89-
converter, dialect, publisher, operations);
92+
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(publisher, aggregateOperations, operations);
9093
jdbcRepositoryFactory.setQueryMappingConfiguration(queryMappingConfiguration);
9194
jdbcRepositoryFactory.setEntityCallbacks(entityCallbacks);
9295
jdbcRepositoryFactory.setBeanFactory(beanFactory);
@@ -138,6 +141,10 @@ public void setJdbcOperations(NamedParameterJdbcOperations operations) {
138141
this.operations = operations;
139142
}
140143

144+
public void setJdbcAggregateOperations(JdbcAggregateOperations jdbcAggregateOperations) {
145+
this.aggregateOperations = jdbcAggregateOperations;
146+
}
147+
141148
public void setConverter(JdbcConverter converter) {
142149

143150
Assert.notNull(converter, "JdbcConverter must not be null");
@@ -156,35 +163,40 @@ public void setBeanFactory(BeanFactory beanFactory) {
156163
@Override
157164
public void afterPropertiesSet() {
158165

159-
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null");
160-
Assert.state(this.converter != null, "RelationalConverter is required and must not be null");
161-
162166
if (this.operations == null) {
163167

164168
Assert.state(beanFactory != null, "If no JdbcOperations are set a BeanFactory must be available");
165169

166170
this.operations = beanFactory.getBean(NamedParameterJdbcOperations.class);
167171
}
168172

169-
if (this.dataAccessStrategy == null) {
173+
if (aggregateOperations == null) {
174+
175+
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null");
176+
Assert.state(this.converter != null, "RelationalConverter is required and must not be null");
177+
178+
if (this.dataAccessStrategy == null) {
179+
180+
Assert.state(beanFactory != null, "If no DataAccessStrategy is set a BeanFactory must be available");
170181

171-
Assert.state(beanFactory != null, "If no DataAccessStrategy is set a BeanFactory must be available");
182+
this.dataAccessStrategy = this.beanFactory.getBeanProvider(DataAccessStrategy.class) //
183+
.getIfAvailable(() -> {
172184

173-
this.dataAccessStrategy = this.beanFactory.getBeanProvider(DataAccessStrategy.class) //
174-
.getIfAvailable(() -> {
185+
Assert.state(this.dialect != null, "Dialect is required and must not be null");
175186

176-
Assert.state(this.dialect != null, "Dialect is required and must not be null");
187+
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(this.mappingContext, this.converter,
188+
this.dialect);
189+
SqlParametersFactory sqlParametersFactory = new SqlParametersFactory(this.mappingContext, this.converter);
190+
InsertStrategyFactory insertStrategyFactory = new InsertStrategyFactory(this.operations, this.dialect);
177191

178-
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(this.mappingContext, this.converter,
179-
this.dialect);
180-
SqlParametersFactory sqlParametersFactory = new SqlParametersFactory(this.mappingContext, this.converter);
181-
InsertStrategyFactory insertStrategyFactory = new InsertStrategyFactory(this.operations, this.dialect);
192+
DataAccessStrategyFactory factory = new DataAccessStrategyFactory(sqlGeneratorSource, this.converter,
193+
this.operations, sqlParametersFactory, insertStrategyFactory);
182194

183-
DataAccessStrategyFactory factory = new DataAccessStrategyFactory(sqlGeneratorSource, this.converter,
184-
this.operations, sqlParametersFactory, insertStrategyFactory);
195+
return factory.create();
196+
});
197+
}
185198

186-
return factory.create();
187-
});
199+
aggregateOperations = new JdbcAggregateTemplate(publisher, converter, dataAccessStrategy);
188200
}
189201

190202
if (this.queryMappingConfiguration == null) {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory
2-
org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider=org.springframework.data.jdbc.repository.config.DialectResolver.DefaultDialectProvider
2+
org.springframework.data.jdbc.dialect.DialectResolver$JdbcDialectProvider=org.springframework.data.jdbc.dialect.DialectResolver.DefaultDialectProvider

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java

+5-23
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,13 @@
2222
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
2323

2424
import java.time.LocalDateTime;
25+
import java.util.*;
2526
import java.util.ArrayList;
26-
import java.util.Collections;
27-
import java.util.HashMap;
28-
import java.util.HashSet;
29-
import java.util.Iterator;
30-
import java.util.List;
31-
import java.util.Map;
32-
import java.util.Objects;
33-
import java.util.Optional;
34-
import java.util.Set;
3527
import java.util.function.Function;
3628
import java.util.stream.IntStream;
3729

3830
import org.junit.jupiter.api.Test;
3931
import org.springframework.beans.factory.annotation.Autowired;
40-
import org.springframework.context.ApplicationEventPublisher;
4132
import org.springframework.context.annotation.Bean;
4233
import org.springframework.context.annotation.Configuration;
4334
import org.springframework.context.annotation.Import;
@@ -51,8 +42,6 @@
5142
import org.springframework.data.domain.PageRequest;
5243
import org.springframework.data.domain.Persistable;
5344
import org.springframework.data.domain.Sort;
54-
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
55-
import org.springframework.data.jdbc.core.convert.JdbcConverter;
5645
import org.springframework.data.jdbc.testing.EnabledOnFeature;
5746
import org.springframework.data.jdbc.testing.IntegrationTest;
5847
import org.springframework.data.jdbc.testing.TestClass;
@@ -917,9 +906,8 @@ void readOnlyGetsLoadedButNotWritten() {
917906

918907
template.save(entity);
919908

920-
assertThat(
921-
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class))
922-
.isEqualTo("from-db");
909+
assertThat(jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(),
910+
String.class)).isEqualTo("from-db");
923911
}
924912

925913
@Test
@@ -1258,7 +1246,8 @@ void recordOfSet() {
12581246
@Test // GH-1656
12591247
void mapWithEnumKey() {
12601248

1261-
EnumMapOwner enumMapOwner = template.save(new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));
1249+
EnumMapOwner enumMapOwner = template.save(
1250+
new EnumMapOwner(null, "OwnerName", Map.of(Color.BLUE, new MapElement("Element"))));
12621251

12631252
Iterable<EnumMapOwner> enumMapOwners = template.findAll(EnumMapOwner.class);
12641253

@@ -2086,7 +2075,6 @@ record Book(String name) {
20862075
record EnumMapOwner(@Id Long id, String name, Map<Color, MapElement> map) {
20872076
}
20882077

2089-
20902078
@Configuration
20912079
@Import(TestConfiguration.class)
20922080
static class Config {
@@ -2095,12 +2083,6 @@ static class Config {
20952083
TestClass testClass() {
20962084
return TestClass.of(JdbcAggregateTemplateIntegrationTests.class);
20972085
}
2098-
2099-
@Bean
2100-
JdbcAggregateOperations operations(ApplicationEventPublisher publisher, RelationalMappingContext context,
2101-
DataAccessStrategy dataAccessStrategy, JdbcConverter converter) {
2102-
return new JdbcAggregateTemplate(publisher, context, converter, dataAccessStrategy);
2103-
}
21042086
}
21052087

21062088
@ContextConfiguration(classes = Config.class)

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/ImmutableAggregateTemplateHsqlIntegrationTests.java

-10
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@
2323
import org.assertj.core.api.SoftAssertions;
2424
import org.junit.jupiter.api.Test;
2525
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.context.ApplicationEventPublisher;
2726
import org.springframework.context.annotation.Bean;
2827
import org.springframework.context.annotation.Configuration;
2928
import org.springframework.context.annotation.Import;
3029
import org.springframework.data.annotation.Id;
31-
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
32-
import org.springframework.data.jdbc.core.convert.JdbcConverter;
3330
import org.springframework.data.jdbc.testing.DatabaseType;
3431
import org.springframework.data.jdbc.testing.EnabledOnDatabase;
3532
import org.springframework.data.jdbc.testing.IntegrationTest;
3633
import org.springframework.data.jdbc.testing.TestConfiguration;
37-
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3834

3935
/**
4036
* Integration tests for {@link JdbcAggregateTemplate} and it's handling of immutable entities.
@@ -602,11 +598,5 @@ static class Config {
602598
Class<?> testClass() {
603599
return ImmutableAggregateTemplateHsqlIntegrationTests.class;
604600
}
605-
606-
@Bean
607-
JdbcAggregateOperations operations(ApplicationEventPublisher publisher, RelationalMappingContext context,
608-
DataAccessStrategy dataAccessStrategy, JdbcConverter converter) {
609-
return new JdbcAggregateTemplate(publisher, context, converter, dataAccessStrategy);
610-
}
611601
}
612602
}

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateSchemaIntegrationTests.java

-12
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@
1616
package org.springframework.data.jdbc.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
2019

2120
import org.junit.jupiter.api.Test;
2221
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.context.ApplicationEventPublisher;
2422
import org.springframework.context.annotation.Bean;
2523
import org.springframework.context.annotation.Configuration;
2624
import org.springframework.context.annotation.Import;
2725
import org.springframework.data.annotation.Id;
28-
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
29-
import org.springframework.data.jdbc.core.convert.JdbcConverter;
30-
import org.springframework.data.jdbc.testing.EnabledOnFeature;
3126
import org.springframework.data.jdbc.testing.IntegrationTest;
3227
import org.springframework.data.jdbc.testing.TestConfiguration;
3328
import org.springframework.data.relational.core.mapping.NamingStrategy;
34-
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3529
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
3630

3731
/**
@@ -86,12 +80,6 @@ Class<?> testClass() {
8680
return JdbcAggregateTemplateSchemaIntegrationTests.class;
8781
}
8882

89-
@Bean
90-
JdbcAggregateOperations operations(ApplicationEventPublisher publisher, RelationalMappingContext context,
91-
DataAccessStrategy dataAccessStrategy, JdbcConverter converter) {
92-
return new JdbcAggregateTemplate(publisher, context, converter, dataAccessStrategy);
93-
}
94-
9583
@Bean
9684
NamingStrategy namingStrategy() {
9785
return new NamingStrategy() {

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void setUp() {
7474
RelationalMappingContext mappingContext = new RelationalMappingContext();
7575
JdbcConverter converter = new MappingJdbcConverter(mappingContext, relationResolver);
7676

77-
template = new JdbcAggregateTemplate(eventPublisher, mappingContext, converter, dataAccessStrategy);
77+
template = new JdbcAggregateTemplate(eventPublisher, converter, dataAccessStrategy);
7878
template.setEntityCallbacks(callbacks);
7979

8080
}

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositoriesIntegrationTests.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@
2727
import org.junit.jupiter.api.Test;
2828
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.beans.factory.annotation.Qualifier;
30+
import org.springframework.context.ApplicationContext;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.ComponentScan;
3233
import org.springframework.context.annotation.Configuration;
3334
import org.springframework.context.annotation.FilterType;
3435
import org.springframework.context.annotation.Import;
3536
import org.springframework.data.annotation.Id;
37+
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
3638
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
3739
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
3840
import org.springframework.data.jdbc.core.convert.DataAccessStrategyFactory;
3941
import org.springframework.data.jdbc.core.convert.InsertStrategyFactory;
4042
import org.springframework.data.jdbc.core.convert.JdbcConverter;
4143
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
4244
import org.springframework.data.jdbc.core.convert.SqlParametersFactory;
45+
import org.springframework.data.jdbc.dialect.DialectResolver;
4346
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
4447
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
4548
import org.springframework.data.jdbc.testing.IntegrationTest;
@@ -71,6 +74,8 @@ public class EnableJdbcRepositoriesIntegrationTests {
7174
static final Field OPERATIONS = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class, "operations");
7275
static final Field DATA_ACCESS_STRATEGY = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class,
7376
"dataAccessStrategy");
77+
static final Field AGGREGATE_OPERATIONS = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class,
78+
"aggregateOperations");
7479
public static final RowMapper DUMMY_ENTITY_ROW_MAPPER = mock(RowMapper.class);
7580
public static final RowMapper STRING_ROW_MAPPER = mock(RowMapper.class);
7681

@@ -81,16 +86,23 @@ public class EnableJdbcRepositoriesIntegrationTests {
8186
@Autowired
8287
@Qualifier("defaultDataAccessStrategy") DataAccessStrategy defaultDataAccessStrategy;
8388
@Autowired
89+
@Qualifier("jdbcAggregateOperations")
90+
JdbcAggregateOperations defaultJdbcAggregateOperations;
91+
@Autowired
8492
@Qualifier("qualifierJdbcOperations") NamedParameterJdbcOperations qualifierJdbcOperations;
8593
@Autowired
8694
@Qualifier("qualifierDataAccessStrategy") DataAccessStrategy qualifierDataAccessStrategy;
95+
@Autowired
96+
@Qualifier("qualifierJdbcAggregateOperations")
97+
JdbcAggregateOperations qualifierJdbcAggregateOperations;
8798

8899
@BeforeAll
89100
public static void setup() {
90101

91102
MAPPER_MAP.setAccessible(true);
92103
OPERATIONS.setAccessible(true);
93104
DATA_ACCESS_STRATEGY.setAccessible(true);
105+
AGGREGATE_OPERATIONS.setAccessible(true);
94106
}
95107

96108
@Test // DATAJDBC-100
@@ -125,6 +137,19 @@ public void jdbcOperationsRef() {
125137
assertThat(dataAccessStrategy).isNotSameAs(defaultDataAccessStrategy).isSameAs(qualifierDataAccessStrategy);
126138
}
127139

140+
@Test
141+
public void jdbcAggregateOperationsRef() {
142+
143+
JdbcAggregateOperations aggregateOperations = (JdbcAggregateOperations) ReflectionUtils.getField(
144+
AGGREGATE_OPERATIONS, factoryBean);
145+
assertThat(aggregateOperations).isNotSameAs(defaultJdbcAggregateOperations)
146+
.isSameAs(qualifierJdbcAggregateOperations);
147+
148+
DataAccessStrategy dataAccessStrategy = (DataAccessStrategy) ReflectionUtils.getField(DATA_ACCESS_STRATEGY,
149+
factoryBean);
150+
assertThat(dataAccessStrategy).isNotSameAs(defaultDataAccessStrategy).isSameAs(qualifierDataAccessStrategy);
151+
}
152+
128153
interface DummyRepository extends CrudRepository<DummyEntity, Long> {
129154

130155
}
@@ -145,6 +170,7 @@ public void setId(Long id) {
145170
@EnableJdbcRepositories(considerNestedRepositories = true,
146171
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DummyRepository.class),
147172
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy",
173+
jdbcAggregateOperationsRef = "qualifierJdbcAggregateOperations",
148174
repositoryBaseClass = DummyRepositoryBaseClass.class)
149175
@Import(TestConfiguration.class)
150176
static class Config {
@@ -167,14 +193,19 @@ DataAccessStrategy defaultDataAccessStrategy(
167193
@Qualifier("namedParameterJdbcTemplate") NamedParameterJdbcOperations template,
168194
RelationalMappingContext context, JdbcConverter converter, Dialect dialect) {
169195
return new DataAccessStrategyFactory(new SqlGeneratorSource(context, converter, dialect), converter, template,
170-
new SqlParametersFactory(context, converter),
171-
new InsertStrategyFactory(template, dialect)).create();
196+
new SqlParametersFactory(context, converter), new InsertStrategyFactory(template, dialect)).create();
172197
}
173198

174199
@Bean
175200
Dialect jdbcDialect(@Qualifier("qualifierJdbcOperations") NamedParameterJdbcOperations operations) {
176201
return DialectResolver.getDialect(operations.getJdbcOperations());
177202
}
203+
204+
@Bean("qualifierJdbcAggregateOperations")
205+
JdbcAggregateOperations jdbcAggregateOperations(ApplicationContext publisher, JdbcConverter converter,
206+
@Qualifier("qualifierDataAccessStrategy") DataAccessStrategy dataAccessStrategy) {
207+
return new JdbcAggregateTemplate(publisher, converter, dataAccessStrategy);
208+
}
178209
}
179210

180211
private static class DummyRepositoryBaseClass<T, ID> implements CrudRepository<T, ID> {

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4141
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
4242
import org.springframework.data.relational.core.dialect.Dialect;
43+
import org.springframework.data.relational.core.dialect.H2Dialect;
4344
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
4445
import org.springframework.data.repository.CrudRepository;
46+
import org.springframework.jdbc.core.ConnectionCallback;
47+
import org.springframework.jdbc.core.JdbcOperations;
4548
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4649
import org.springframework.test.util.ReflectionTestUtils;
4750

@@ -66,6 +69,11 @@ public class JdbcRepositoryFactoryBeanUnitTests {
6669
@Mock(answer = Answers.RETURNS_DEEP_STUBS) ListableBeanFactory beanFactory;
6770
@Mock Dialect dialect;
6871

72+
@Mock
73+
JdbcOperations operations;
74+
@Mock
75+
NamedParameterJdbcOperations namedParameterJdbcOperations;
76+
6977
RelationalMappingContext mappingContext;
7078

7179
@BeforeEach
@@ -76,7 +84,9 @@ public void setUp() {
7684
// Setup standard configuration
7785
factoryBean = new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class);
7886

79-
when(beanFactory.getBean(NamedParameterJdbcOperations.class)).thenReturn(mock(NamedParameterJdbcOperations.class));
87+
when(operations.execute(any(ConnectionCallback.class))).thenReturn(H2Dialect.INSTANCE);
88+
when(namedParameterJdbcOperations.getJdbcOperations()).thenReturn(operations);
89+
when(beanFactory.getBean(NamedParameterJdbcOperations.class)).thenReturn(namedParameterJdbcOperations);
8090

8191
ObjectProvider<DataAccessStrategy> provider = mock(ObjectProvider.class);
8292
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(provider);

‎spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.factory.BeanFactory;
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.beans.factory.annotation.Qualifier;
29+
import org.springframework.context.ApplicationContext;
2930
import org.springframework.context.ApplicationEventPublisher;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.ComponentScan;
@@ -34,11 +35,13 @@
3435
import org.springframework.context.annotation.Primary;
3536
import org.springframework.context.annotation.Profile;
3637
import org.springframework.data.convert.CustomConversions;
38+
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
39+
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
3740
import org.springframework.data.jdbc.core.convert.*;
3841
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
3942
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4043
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
41-
import org.springframework.data.jdbc.repository.config.DialectResolver;
44+
import org.springframework.data.jdbc.dialect.DialectResolver;
4245
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
4346
import org.springframework.data.mapping.model.SimpleTypeHolder;
4447
import org.springframework.data.relational.core.dialect.Dialect;
@@ -169,6 +172,12 @@ Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
169172
return DialectResolver.getDialect(operations.getJdbcOperations());
170173
}
171174

175+
@Bean
176+
JdbcAggregateOperations jdbcAggregateOperations(ApplicationContext publisher, JdbcConverter converter,
177+
@Qualifier("defaultDataAccessStrategy") DataAccessStrategy dataAccessStrategy) {
178+
return new JdbcAggregateTemplate(publisher, converter, dataAccessStrategy);
179+
}
180+
172181
@Lazy
173182
@Bean
174183
TestDatabaseFeatures features(NamedParameterJdbcOperations operations) {

0 commit comments

Comments
 (0)
Please sign in to comment.