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.
This commit is contained in:
committed by
Mark Paluch
parent
b74c82c724
commit
8f495891a2
@@ -15,10 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.repository.config;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -75,4 +79,12 @@ public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtens
|
||||
.filter(StringUtils::hasText) //
|
||||
.ifPresent(s -> builder.addPropertyReference("dataAccessStrategy", s));
|
||||
}
|
||||
|
||||
/**
|
||||
* In strict mode only domain types having a {@link Table} annotation get a repository.
|
||||
*/
|
||||
@Override
|
||||
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
|
||||
return Collections.singleton(Table.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
|
||||
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
|
||||
|
||||
SimpleJdbcRepository<?, Object> repository = new SimpleJdbcRepository<>(template,
|
||||
context.getPersistentEntity(repositoryInformation.getDomainType()));
|
||||
context.getRequiredPersistentEntity(repositoryInformation.getDomainType()));
|
||||
|
||||
if (entityCallbacks != null) {
|
||||
template.setEntityCallbacks(entityCallbacks);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.repository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
||||
|
||||
@@ -33,7 +33,9 @@ import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
|
||||
@@ -15,7 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.repository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -43,9 +46,6 @@ import org.springframework.test.context.junit4.rules.SpringClassRule;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Very simple use cases for creation and usage of {@link ResultSetExtractor}s in JdbcRepository.
|
||||
*
|
||||
@@ -66,7 +66,7 @@ public class JdbcRepositoryQueryMappingConfigurationIntegrationTests {
|
||||
Class<?> testClass() {
|
||||
return JdbcRepositoryQueryMappingConfigurationIntegrationTests.class;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
QueryMappingConfiguration mappers() {
|
||||
return new DefaultQueryMappingConfiguration();
|
||||
@@ -78,7 +78,7 @@ public class JdbcRepositoryQueryMappingConfigurationIntegrationTests {
|
||||
|
||||
@Autowired NamedParameterJdbcTemplate template;
|
||||
@Autowired CarRepository carRepository;
|
||||
|
||||
|
||||
@Test // DATAJDBC-290
|
||||
public void customFindAllCarsUsesConfiguredResultSetExtractor() {
|
||||
|
||||
@@ -88,28 +88,27 @@ public class JdbcRepositoryQueryMappingConfigurationIntegrationTests {
|
||||
assertThat(cars).hasSize(1);
|
||||
assertThat(cars).allMatch(car -> CAR_MODEL.equals(car.getModel()));
|
||||
}
|
||||
|
||||
|
||||
interface CarRepository extends CrudRepository<Car, Long> {
|
||||
|
||||
@Query(value = "select * from car", resultSetExtractorClass = CarResultSetExtractor.class)
|
||||
List<Car> customFindAll();
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class Car {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
@Id private Long id;
|
||||
private String model;
|
||||
}
|
||||
|
||||
|
||||
static class CarResultSetExtractor implements ResultSetExtractor<List<Car>> {
|
||||
|
||||
@Override
|
||||
public List<Car> extractData(ResultSet rs) throws SQLException, DataAccessException {
|
||||
return Arrays.asList(new Car(1L, CAR_MODEL));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.repository.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfiguration;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JdbcRepositoryConfigExtension}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class JdbcRepositoryConfigExtensionUnitTests {
|
||||
|
||||
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
|
||||
ResourceLoader loader = new PathMatchingResourcePatternResolver();
|
||||
Environment environment = new StandardEnvironment();
|
||||
BeanDefinitionRegistry registry = new DefaultListableBeanFactory();
|
||||
|
||||
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
|
||||
EnableJdbcRepositories.class, loader, environment, registry);
|
||||
|
||||
@Test // DATAJPA-437
|
||||
public void isStrictMatchOnlyIfDomainTypeIsAnnotatedWithDocument() {
|
||||
|
||||
JdbcRepositoryConfigExtension extension = new JdbcRepositoryConfigExtension();
|
||||
|
||||
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs = extension
|
||||
.getRepositoryConfigurations(configurationSource, loader, true);
|
||||
|
||||
assertThat(configs).extracting(config -> config.getRepositoryInterface())
|
||||
.containsExactly(SampleRepository.class.getName());
|
||||
}
|
||||
|
||||
@EnableJdbcRepositories(considerNestedRepositories = true)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Table
|
||||
static class Sample {}
|
||||
|
||||
interface SampleRepository extends Repository<Sample, Long> {}
|
||||
|
||||
static class Unannotated {}
|
||||
|
||||
interface UnannotatedRepository extends Repository<Unannotated, Long> {}
|
||||
}
|
||||
Reference in New Issue
Block a user