Skip to content

Commit 593a19a

Browse files
committed
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 2f1f6d0 commit 593a19a

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed
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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
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.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
24+
import org.springframework.data.relational.core.mapping.Table;
2125
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
2226

2327
/**
@@ -55,4 +59,11 @@ protected String getModulePrefix() {
5559
return getModuleName().toLowerCase(Locale.US);
5660
}
5761

62+
/**
63+
* In strict mode only domain types having a {@link Table} annotation get a repository.
64+
*/
65+
@Override
66+
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
67+
return Collections.singleton(Table.class);
68+
}
5869
}

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
@@ -106,7 +106,7 @@ protected Object getTargetRepository(RepositoryInformation repositoryInformation
106106

107107
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
108108

109-
return new SimpleJdbcRepository<>(template, context.getPersistentEntity(repositoryInformation.getDomainType()));
109+
return new SimpleJdbcRepository<>(template, context.getRequiredPersistentEntity(repositoryInformation.getDomainType()));
110110
}
111111

112112
/*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.ComponentScan;
3030
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.context.annotation.FilterType;
3132
import org.springframework.context.annotation.Import;
3233
import org.springframework.data.annotation.Id;
3334
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import org.springframework.beans.factory.annotation.Autowired;
3535
import org.springframework.context.ApplicationListener;
3636
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.ComponentScan;
3738
import org.springframework.context.annotation.Configuration;
39+
import org.springframework.context.annotation.FilterType;
3840
import org.springframework.context.annotation.Import;
3941
import org.springframework.data.annotation.Id;
4042
import org.springframework.data.annotation.PersistenceConstructor;
Lines changed: 76 additions & 0 deletions
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)