Skip to content

Commit 53a7a01

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-437 - In strict mode we only claim repositories for domain types with @table annotation.
Before this change Spring Data JDBC didn't specify any identifying annotation and therefore would claim all or no repository depending on the the version of Spring Data Commons. Also added the RepositoryFactorySupport to spring.factory in order to support detection of multiple RepositoryFactorySupport implementations on the classpath. Related ticket: DATACMNS-1596. Original pull request: #177.
1 parent b602225 commit 53a7a01

File tree

8 files changed

+105
-14
lines changed

8 files changed

+105
-14
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.config;
1717

18+
import java.lang.annotation.Annotation;
19+
import java.util.Collection;
20+
import java.util.Collections;
1821
import java.util.Locale;
1922

2023
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2124
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
25+
import org.springframework.data.relational.core.mapping.Table;
2226
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
2327
import org.springframework.data.repository.config.RepositoryConfigurationSource;
2428
import org.springframework.util.StringUtils;
@@ -75,4 +79,12 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
7579
.filter(StringUtils::hasText) //
7680
.ifPresent(s -> builder.addPropertyReference("dataAccessStrategy", s));
7781
}
82+
83+
/**
84+
* In strict mode only domain types having a {@link Table} annotation get a repository.
85+
*/
86+
@Override
87+
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
88+
return Collections.singleton(Table.class);
89+
}
7890
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected Object getTargetRepository(RepositoryInformation repositoryInformation
120120
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
121121

122122
SimpleJdbcRepository<?, Object> repository = new SimpleJdbcRepository<>(template,
123-
context.getPersistentEntity(repositoryInformation.getDomainType()));
123+
context.getRequiredPersistentEntity(repositoryInformation.getDomainType()));
124124

125125
if (entityCallbacks != null) {
126126
template.setEntityCallbacks(entityCallbacks);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.repository;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.ClassRule;
2121
import org.junit.Rule;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.Bean;
3232
import org.springframework.context.annotation.ComponentScan;
3333
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.context.annotation.FilterType;
3435
import org.springframework.context.annotation.Import;
3536
import org.springframework.data.annotation.Id;
3637
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.context.ApplicationListener;
3535
import org.springframework.context.annotation.Bean;
36+
import org.springframework.context.annotation.ComponentScan;
3637
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.context.annotation.FilterType;
3739
import org.springframework.context.annotation.Import;
3840
import org.springframework.data.annotation.Id;
3941
import org.springframework.data.annotation.PersistenceConstructor;

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

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

18-
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import lombok.AllArgsConstructor;
21+
import lombok.Data;
1922

2023
import java.sql.ResultSet;
2124
import java.sql.SQLException;
@@ -43,9 +46,6 @@
4346
import org.springframework.test.context.junit4.rules.SpringMethodRule;
4447
import org.springframework.transaction.annotation.Transactional;
4548

46-
import lombok.AllArgsConstructor;
47-
import lombok.Data;
48-
4949
/**
5050
* Very simple use cases for creation and usage of {@link ResultSetExtractor}s in JdbcRepository.
5151
*
@@ -66,7 +66,7 @@ static class Config {
6666
Class<?> testClass() {
6767
return JdbcRepositoryQueryMappingConfigurationIntegrationTests.class;
6868
}
69-
69+
7070
@Bean
7171
QueryMappingConfiguration mappers() {
7272
return new DefaultQueryMappingConfiguration();
@@ -78,7 +78,7 @@ QueryMappingConfiguration mappers() {
7878

7979
@Autowired NamedParameterJdbcTemplate template;
8080
@Autowired CarRepository carRepository;
81-
81+
8282
@Test // DATAJDBC-290
8383
public void customFindAllCarsUsesConfiguredResultSetExtractor() {
8484

@@ -88,28 +88,27 @@ public void customFindAllCarsUsesConfiguredResultSetExtractor() {
8888
assertThat(cars).hasSize(1);
8989
assertThat(cars).allMatch(car -> CAR_MODEL.equals(car.getModel()));
9090
}
91-
91+
9292
interface CarRepository extends CrudRepository<Car, Long> {
9393

9494
@Query(value = "select * from car", resultSetExtractorClass = CarResultSetExtractor.class)
9595
List<Car> customFindAll();
9696
}
97-
97+
9898
@Data
9999
@AllArgsConstructor
100100
static class Car {
101101

102-
@Id
103-
private Long id;
102+
@Id private Long id;
104103
private String model;
105104
}
106-
105+
107106
static class CarResultSetExtractor implements ResultSetExtractor<List<Car>> {
108107

109108
@Override
110109
public List<Car> extractData(ResultSet rs) throws SQLException, DataAccessException {
111110
return Arrays.asList(new Car(1L, CAR_MODEL));
112111
}
113-
112+
114113
}
115114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.repository.config;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.Collection;
21+
22+
import org.junit.Test;
23+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
24+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
25+
import org.springframework.core.env.Environment;
26+
import org.springframework.core.env.StandardEnvironment;
27+
import org.springframework.core.io.ResourceLoader;
28+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
29+
import org.springframework.core.type.StandardAnnotationMetadata;
30+
import org.springframework.data.relational.core.mapping.Table;
31+
import org.springframework.data.repository.Repository;
32+
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
33+
import org.springframework.data.repository.config.RepositoryConfiguration;
34+
import org.springframework.data.repository.config.RepositoryConfigurationSource;
35+
36+
/**
37+
* Unit tests for {@link JdbcRepositoryConfigExtension}.
38+
*
39+
* @author Jens Schauder
40+
*/
41+
public class JdbcRepositoryConfigExtensionUnitTests {
42+
43+
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
44+
ResourceLoader loader = new PathMatchingResourcePatternResolver();
45+
Environment environment = new StandardEnvironment();
46+
BeanDefinitionRegistry registry = new DefaultListableBeanFactory();
47+
48+
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
49+
EnableJdbcRepositories.class, loader, environment, registry);
50+
51+
@Test // DATAJPA-437
52+
public void isStrictMatchOnlyIfDomainTypeIsAnnotatedWithDocument() {
53+
54+
JdbcRepositoryConfigExtension extension = new JdbcRepositoryConfigExtension();
55+
56+
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs = extension
57+
.getRepositoryConfigurations(configurationSource, loader, true);
58+
59+
assertThat(configs).extracting(config -> config.getRepositoryInterface())
60+
.containsExactly(SampleRepository.class.getName());
61+
}
62+
63+
@EnableJdbcRepositories(considerNestedRepositories = true)
64+
static class Config {
65+
66+
}
67+
68+
@Table
69+
static class Sample {}
70+
71+
interface SampleRepository extends Repository<Sample, Long> {}
72+
73+
static class Unannotated {}
74+
75+
interface UnannotatedRepository extends Repository<Unannotated, Long> {}
76+
}

0 commit comments

Comments
 (0)