Skip to content

Commit d3f4bce

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-572 - Enable specification of a repository base class.
Added the field to `EnableJdbcRepository` and respect it's value by instantiating the repository base class reflectively. Original pull request: spring-projects#235.
1 parent b28e4e0 commit d3f4bce

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.annotation.ComponentScan.Filter;
2727
import org.springframework.context.annotation.Import;
2828
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
29+
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
2930

3031
/**
3132
* Annotation to enable JDBC repositories. Will scan the package of the annotated configuration class for Spring Data
@@ -112,4 +113,13 @@
112113
* be used to create repositories discovered through this annotation. Defaults to {@code defaultDataAccessStrategy}.
113114
*/
114115
String dataAccessStrategyRef() default "";
116+
117+
/**
118+
* Configure the repository base class to be used to create repository proxies for this particular configuration.
119+
*
120+
* @return
121+
* @since 2.1
122+
*/
123+
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
124+
115125
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.support;
1717

18+
import java.io.Serializable;
1819
import java.util.Optional;
1920

2021
import org.springframework.context.ApplicationEventPublisher;
@@ -26,6 +27,7 @@
2627
import org.springframework.data.relational.core.dialect.Dialect;
2728
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
2829
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
30+
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
2931
import org.springframework.data.repository.core.EntityInformation;
3032
import org.springframework.data.repository.core.RepositoryInformation;
3133
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -115,14 +117,13 @@ protected Object getTargetRepository(RepositoryInformation repositoryInformation
115117

116118
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
117119

118-
SimpleJdbcRepository<?, Object> repository = new SimpleJdbcRepository<>(template,
119-
context.getRequiredPersistentEntity(repositoryInformation.getDomainType()));
120-
121120
if (entityCallbacks != null) {
122121
template.setEntityCallbacks(entityCallbacks);
123122
}
124123

125-
return repository;
124+
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(repositoryInformation.getDomainType());
125+
126+
return getTargetRepositoryViaReflection(repositoryInformation.getRepositoryBaseClass(), template, persistentEntity);
126127
}
127128

128129
/*

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import lombok.Data;
2222

2323
import java.lang.reflect.Field;
24+
import java.util.Optional;
2425

2526
import javax.sql.DataSource;
2627

@@ -33,13 +34,15 @@
3334
import org.springframework.context.annotation.ComponentScan;
3435
import org.springframework.context.annotation.FilterType;
3536
import org.springframework.data.annotation.Id;
37+
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
3638
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
3739
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
3840
import org.springframework.data.jdbc.core.convert.JdbcConverter;
3941
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
4042
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
4143
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositoriesIntegrationTests.TestConfiguration;
4244
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
45+
import org.springframework.data.mapping.PersistentEntity;
4346
import org.springframework.data.relational.core.dialect.Dialect;
4447
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
4548
import org.springframework.data.repository.CrudRepository;
@@ -90,9 +93,10 @@ public void repositoryGetsPickedUp() {
9093

9194
assertThat(repository).isNotNull();
9295

93-
Iterable<DummyEntity> all = repository.findAll();
96+
long count = repository.count();
9497

95-
assertThat(all).isNotNull();
98+
// the custom base class has a result of 23 hard wired.
99+
assertThat(count).isEqualTo(23L);
96100
}
97101

98102
@Test // DATAJDBC-166
@@ -128,7 +132,8 @@ static class DummyEntity {
128132
@ComponentScan("org.springframework.data.jdbc.testing")
129133
@EnableJdbcRepositories(considerNestedRepositories = true,
130134
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DummyRepository.class),
131-
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy")
135+
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy",
136+
repositoryBaseClass = DummyRepositoryBaseClass.class)
132137
static class TestConfiguration {
133138

134139
@Bean
@@ -162,4 +167,55 @@ Dialect jdbcDialect(@Qualifier("qualifierJdbcOperations") NamedParameterJdbcOper
162167
return DialectResolver.getDialect(operations.getJdbcOperations());
163168
}
164169
}
170+
171+
private static class DummyRepositoryBaseClass{
172+
173+
DummyRepositoryBaseClass(JdbcAggregateTemplate template, PersistentEntity<?,?> persistentEntity) {
174+
175+
}
176+
177+
public Object save(Object o) {
178+
return null;
179+
}
180+
181+
public Iterable saveAll(Iterable iterable) {
182+
return null;
183+
}
184+
185+
public Optional findById(Object o) {
186+
return Optional.empty();
187+
}
188+
189+
public boolean existsById(Object o) {
190+
return false;
191+
}
192+
193+
public Iterable findAll() {
194+
return null;
195+
}
196+
197+
public Iterable findAllById(Iterable iterable) {
198+
return null;
199+
}
200+
201+
public long count() {
202+
return 23L;
203+
}
204+
205+
public void deleteById(Object o) {
206+
207+
}
208+
209+
public void delete(Object o) {
210+
211+
}
212+
213+
public void deleteAll(Iterable iterable) {
214+
215+
}
216+
217+
public void deleteAll() {
218+
219+
}
220+
}
165221
}

0 commit comments

Comments
 (0)