diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositories.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositories.java index c5e169450..10752cc8d 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositories.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositories.java @@ -136,6 +136,8 @@ public @interface EnableJdbcRepositories { /** * Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to * {@link QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}. + * + * @since 2.1 */ QueryLookupStrategy.Key queryLookupStrategy() default QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND; } diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java index 4fbdd69f8..a854b99c3 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java @@ -99,6 +99,7 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy { * {@link QueryLookupStrategy} to create a query from the method name. * * @author Diego Krupitza + * @since 2.4 */ static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy { @@ -125,6 +126,7 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy { * {@link org.springframework.data.jdbc.repository.query.Query} annotation followed by a JPA named query lookup. * * @author Diego Krupitza + * @since 2.4 */ static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy { @@ -150,7 +152,7 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy { StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryMethod, getOperations(), this::createMapper, getConverter()); - query.setBeanFactory(getBeanfactory()); + query.setBeanFactory(getBeanFactory()); return query; } @@ -163,12 +165,9 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy { * {@link QueryLookupStrategy} to try to detect a declared query first ( * {@link org.springframework.data.jdbc.repository.query.Query}, JDBC named query). In case none is found we fall back * on query creation. - *
- * Modified based on original source: {@link org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy}
*
- * @author Oliver Gierke
- * @author Thomas Darimont
* @author Diego Krupitza
+ * @since 2.4
*/
static class CreateIfNotFoundQueryLookupStrategy extends JdbcQueryLookupStrategy {
@@ -181,7 +180,7 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy {
* @param createStrategy must not be {@literal null}.
* @param lookupStrategy must not be {@literal null}.
*/
- public CreateIfNotFoundQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
+ CreateIfNotFoundQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
@Nullable BeanFactory beanfactory, CreateQueryLookupStrategy createStrategy,
@@ -228,12 +227,12 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy {
* @param dialect must not be {@literal null}
* @param queryMappingConfiguration must not be {@literal null}
* @param operations must not be {@literal null}
- * @param beanfactory may be {@literal null}
+ * @param beanFactory may be {@literal null}
*/
public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPublisher publisher,
@Nullable EntityCallbacks callbacks, RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
- @Nullable BeanFactory beanfactory) {
+ @Nullable BeanFactory beanFactory) {
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
Assert.notNull(context, "RelationalMappingContextPublisher must not be null");
@@ -243,10 +242,10 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy {
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy(publisher, callbacks, context,
- converter, dialect, queryMappingConfiguration, operations, beanfactory);
+ converter, dialect, queryMappingConfiguration, operations, beanFactory);
DeclaredQueryLookupStrategy declaredQueryLookupStrategy = new DeclaredQueryLookupStrategy(publisher, callbacks,
- context, converter, dialect, queryMappingConfiguration, operations, beanfactory);
+ context, converter, dialect, queryMappingConfiguration, operations, beanFactory);
Key cleanedKey = key != null ? key : Key.CREATE_IF_NOT_FOUND;
@@ -259,34 +258,30 @@ abstract class JdbcQueryLookupStrategy implements QueryLookupStrategy {
return declaredQueryLookupStrategy;
case CREATE_IF_NOT_FOUND:
return new CreateIfNotFoundQueryLookupStrategy(publisher, callbacks, context, converter, dialect,
- queryMappingConfiguration, operations, beanfactory, createQueryLookupStrategy, declaredQueryLookupStrategy);
+ queryMappingConfiguration, operations, beanFactory, createQueryLookupStrategy, declaredQueryLookupStrategy);
default:
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
}
}
- protected ApplicationEventPublisher getPublisher() {
- return publisher;
- }
-
- protected RelationalMappingContext getContext() {
+ RelationalMappingContext getContext() {
return context;
}
- protected JdbcConverter getConverter() {
+ JdbcConverter getConverter() {
return converter;
}
- protected Dialect getDialect() {
+ Dialect getDialect() {
return dialect;
}
- protected NamedParameterJdbcOperations getOperations() {
+ NamedParameterJdbcOperations getOperations() {
return operations;
}
@Nullable
- protected BeanFactory getBeanfactory() {
+ BeanFactory getBeanFactory() {
return beanfactory;
}
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyTests.java
similarity index 89%
rename from spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyIntegrationTests.java
rename to spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyTests.java
index 41e5646c9..8193b0c65 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyIntegrationTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/AbstractJdbcRepositoryLookUpStrategyTests.java
@@ -29,18 +29,19 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
- * Base class to test @EnableJdbcRepositories(queryLookupStrategy = ...) Tests based on logic from
- * {@link org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy}
+ * Base class to test @EnableJdbcRepositories(queryLookupStrategy = ...)
*
* @author Diego Krupitza
+ * @since 2.4
*/
-abstract class AbstractJdbcRepositoryLookUpStrategyIntegrationTests {
+abstract class AbstractJdbcRepositoryLookUpStrategyTests {
@Autowired protected OnesRepository onesRepository;
@Autowired NamedParameterJdbcTemplate template;
@Autowired RelationalMappingContext context;
- protected void insertTestInstances() {
+ void insertTestInstances() {
+
AggregateOne firstAggregate = new AggregateOne("Diego");
AggregateOne secondAggregate = new AggregateOne("Franz");
AggregateOne thirdAggregate = new AggregateOne("Daniela");
@@ -48,7 +49,8 @@ abstract class AbstractJdbcRepositoryLookUpStrategyIntegrationTests {
onesRepository.saveAll(Arrays.asList(firstAggregate, secondAggregate, thirdAggregate));
}
- protected void callDeclaredQuery(String name, int expectedSize, String... expectedNames) {
+ void callDeclaredQuery(String name, int expectedSize, String... expectedNames) {
+
insertTestInstances();
List@EnableJdbcRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND) works as
- * intended. Tests based on logic from
- * {@link org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy.CreateIfNotFoundQueryLookupStrategy}
+ * intended.
*
* @author Diego Krupitza
+ * @author Jens Schauder
*/
@ContextConfiguration
@Transactional
-@ActiveProfiles("hsql")
@ExtendWith(SpringExtension.class)
-class JdbcRepositoryCreateIfNotFoundLookUpStrategyIntegrationTests
- extends AbstractJdbcRepositoryLookUpStrategyIntegrationTests {
+class JdbcRepositoryCreateIfNotFoundLookUpStrategyTests
+ extends AbstractJdbcRepositoryLookUpStrategyTests {
- @Test
+ @Test // GH-1043
void declaredQueryShouldWork() {
onesRepository.deleteAll();
callDeclaredQuery("D", 2, "Diego", "Daniela");
}
- @Test
+ @Test // GH-1043
void derivedQueryShouldWork() {
onesRepository.deleteAll();
callDerivedQuery();
@@ -64,7 +62,7 @@ class JdbcRepositoryCreateIfNotFoundLookUpStrategyIntegrationTests
@EnableJdbcRepositories(considerNestedRepositories = true,
queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND,
includeFilters = @ComponentScan.Filter(
- value = AbstractJdbcRepositoryLookUpStrategyIntegrationTests.OnesRepository.class,
+ value = AbstractJdbcRepositoryLookUpStrategyTests.OnesRepository.class,
type = FilterType.ASSIGNABLE_TYPE))
static class Config {
@@ -72,8 +70,7 @@ class JdbcRepositoryCreateIfNotFoundLookUpStrategyIntegrationTests
@Bean
Class> testClass() {
- return AbstractJdbcRepositoryLookUpStrategyIntegrationTests.class;
+ return AbstractJdbcRepositoryLookUpStrategyTests.class;
}
}
-
}
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyTests.java
similarity index 84%
rename from spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyIntegrationTests.java
rename to spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyTests.java
index 59371955e..77d7f8109 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyIntegrationTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCreateLookUpStrategyTests.java
@@ -34,26 +34,25 @@ import org.springframework.transaction.annotation.Transactional;
/**
* Test to verify that @EnableJdbcRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE) works
- * as intended. Tests based on logic from
- * {@link org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy.CreateQueryLookupStrategy}
+ * as intended.
*
* @author Diego Krupitza
+ * @author Jens Schauder
*/
@ContextConfiguration
@Transactional
-@ActiveProfiles("hsql")
@ExtendWith(SpringExtension.class)
-class JdbcRepositoryCreateLookUpStrategyIntegrationTests extends AbstractJdbcRepositoryLookUpStrategyIntegrationTests {
+class JdbcRepositoryCreateLookUpStrategyTests extends AbstractJdbcRepositoryLookUpStrategyTests {
- @Test
+ @Test // GH-1043
void declaredQueryShouldWork() {
onesRepository.deleteAll();
- // here the declared query will use the dervice query which does something totally different
+ // here the declared query will use the derived query which does something totally different
callDeclaredQuery("D", 0);
}
- @Test
+ @Test // GH-1043
void derivedQueryShouldWork() {
onesRepository.deleteAll();
callDerivedQuery();
@@ -69,7 +68,7 @@ class JdbcRepositoryCreateLookUpStrategyIntegrationTests extends AbstractJdbcRep
@Bean
Class> testClass() {
- return AbstractJdbcRepositoryLookUpStrategyIntegrationTests.class;
+ return AbstractJdbcRepositoryLookUpStrategyTests.class;
}
}
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyTests.java
similarity index 76%
rename from spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyIntegrationTests.java
rename to spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyTests.java
index 40fd53eb6..83ac2f97a 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyIntegrationTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryDeclaredLookUpStrategyTests.java
@@ -18,20 +18,19 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
/**
* Test to verify that
* @EnableJdbcRepositories(queryLookupStrategy = QueryLookupStrategy.Key.USE_DECLARED_QUERY) works as
- * intended. Tests based on logic from
- * {@link org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy.DeclaredQueryLookupStrategy}
+ * intended.
*
* @author Diego Krupitza
*/
-class JdbcRepositoryDeclaredLookUpStrategyIntegrationTests
- extends AbstractJdbcRepositoryLookUpStrategyIntegrationTests {
+class JdbcRepositoryDeclaredLookUpStrategyTests
+ extends AbstractJdbcRepositoryLookUpStrategyTests {
- @Test
+ @Test // GH-1043
void contextCannotByCreatedDueToFindByNameNotDeclaredQuery() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
- context.register(JdbcRepositoryDeclaredLookUpStrategyIntegrationTests.Config.class);
+ context.register(JdbcRepositoryDeclaredLookUpStrategyTests.Config.class);
assertThatThrownBy(() -> {
context.refresh();
@@ -45,7 +44,7 @@ class JdbcRepositoryDeclaredLookUpStrategyIntegrationTests
@EnableJdbcRepositories(considerNestedRepositories = true,
queryLookupStrategy = QueryLookupStrategy.Key.USE_DECLARED_QUERY,
includeFilters = @ComponentScan.Filter(
- value = AbstractJdbcRepositoryLookUpStrategyIntegrationTests.OnesRepository.class,
+ value = AbstractJdbcRepositoryLookUpStrategyTests.OnesRepository.class,
type = FilterType.ASSIGNABLE_TYPE))
static class Config {
@@ -53,8 +52,7 @@ class JdbcRepositoryDeclaredLookUpStrategyIntegrationTests
@Bean
Class> testClass() {
- return AbstractJdbcRepositoryLookUpStrategyIntegrationTests.class;
+ return AbstractJdbcRepositoryLookUpStrategyTests.class;
}
}
-
}
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategyUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategyUnitTests.java
index 4b2187c77..170c05200 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategyUnitTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategyUnitTests.java
@@ -111,7 +111,7 @@ class JdbcQueryLookupStrategyUnitTests {
verify(operations).queryForObject(eq("some SQL"), any(SqlParameterSource.class), any(RowMapper.class));
}
- @Test
+ @Test // GH-1043
void shouldFailOnMissingDeclaredQuery() {
RowMapper extends NumberFormat> numberFormatMapper = mock(RowMapper.class);
@@ -139,6 +139,7 @@ class JdbcQueryLookupStrategyUnitTests {
}
private static Stream