Skip to content

DATAJDBC-437 - In strict mode we only claim repositories for domain types with @Table annotation. #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,4 +79,12 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*/
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;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
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.core.mapping.AggregateReference;
Expand Down Expand Up @@ -56,7 +58,8 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {

@Configuration
@Import(TestConfiguration.class)
@EnableJdbcRepositories(considerNestedRepositories = true)
@EnableJdbcRepositories(includeFilters = @Filter(type = FilterType.REGEX,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should improve handling for non-existent entity types in JdbcRepositoryFactory.getTargetRepository(…) (e.g. calling getRequiredPersistentEntity(…) instead of getPersistentEntity(…)).

java.lang.IllegalArgumentException: entity is marked non-null but is null
	at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.<init>(SimpleJdbcRepository.java:36)

is hard to debug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pattern = ".*\\.JdbcRepositoryCrossAggregateHsqlIntegrationTests\\$.*"), considerNestedRepositories = true)
static class Config {

@Autowired JdbcRepositoryFactory factory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -145,7 +146,8 @@ static class ImmutableWithManualIdEntity {

@Configuration
@ComponentScan("org.springframework.data.jdbc.testing")
@EnableJdbcRepositories(considerNestedRepositories = true)
@EnableJdbcRepositories(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = ".*\\.JdbcRepositoryIdGenerationIntegrationTests\\$.*"), considerNestedRepositories = true)
static class TestConfiguration {

AtomicLong lastId = new AtomicLong(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
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;
Expand Down Expand Up @@ -185,7 +187,8 @@ private interface LogRepository extends CrudRepository<Log, Long> {}

@Configuration
@Import(TestConfiguration.class)
@EnableJdbcRepositories(considerNestedRepositories = true)
@EnableJdbcRepositories(includeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = ".*\\.JdbcRepositoryManipulateDbActionsIntegrationTests\\$.*"), considerNestedRepositories = true)
static class Config {

static long lastLogId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +30,9 @@
import org.junit.Test;
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.dao.DataAccessException;
import org.springframework.data.annotation.Id;
Expand All @@ -43,9 +48,6 @@
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.
*
Expand All @@ -59,14 +61,17 @@ public class JdbcRepositoryQueryMappingConfigurationIntegrationTests {

@Configuration
@Import(TestConfiguration.class)
@EnableJdbcRepositories(considerNestedRepositories = true)
@EnableJdbcRepositories(
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = ".*\\.JdbcRepositoryQueryMappingConfigurationIntegrationTests\\$.*"),
considerNestedRepositories = true)
static class Config {

@Bean
Class<?> testClass() {
return JdbcRepositoryQueryMappingConfigurationIntegrationTests.class;
}

@Bean
QueryMappingConfiguration mappers() {
return new DefaultQueryMappingConfiguration();
Expand All @@ -78,7 +83,7 @@ QueryMappingConfiguration mappers() {

@Autowired NamedParameterJdbcTemplate template;
@Autowired CarRepository carRepository;

@Test // DATAJDBC-290
public void customFindAllCarsUsesConfiguredResultSetExtractor() {

Expand All @@ -88,28 +93,27 @@ public void customFindAllCarsUsesConfiguredResultSetExtractor() {
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));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Primary;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
Expand Down Expand Up @@ -258,7 +260,9 @@ static class DummyEntity {
}

@ComponentScan("org.springframework.data.jdbc.testing")
@EnableJdbcRepositories(considerNestedRepositories = true)
@EnableJdbcRepositories(
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*\\.EnableJdbcAuditingHsqlIntegrationTests\\$.*"),
considerNestedRepositories = true)
static class TestConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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
* @since 1.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: No need for @since in test classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
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> {}

interface UnannotatedRepository extends Repository<Object, Long> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we don't use Object here to avoid the need to touch other, unrelated tests. Let's use a different type here.

}
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
<version>1.2.0.DATAJDBC-437-SNAPSHOT</version>
</parent>

<properties>
Expand Down