Skip to content

Commit 09fd0fc

Browse files
committed
DATAJDBC-234 - Fixes the default query naming and polishing.
The default name now does include the name of the domain class. The default name now is no longer used when no query with the name specified in the `@Query` annotation is found. Simplified the unit tests by extracting common functionality and improving naming. Simplified test configuration. Original pull request: #180.
1 parent 93272c8 commit 09fd0fc

File tree

10 files changed

+116
-190
lines changed

10 files changed

+116
-190
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
/**
4848
* The named query to be used. If not defined, the name of
49-
* {@code $ domainClass}.${queryMethodName}} will be used.
49+
* {@code ${domainClass}.${queryMethodName}} will be used.
5050
*/
5151
String name() default "";
5252

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
5454
private final JdbcConverter converter;
5555
private final QueryMappingConfiguration queryMappingConfiguration;
5656
private final NamedParameterJdbcOperations operations;
57-
5857

5958
/*
6059
* (non-Javadoc)
@@ -64,7 +63,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
6463
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
6564
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
6665

67-
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory,namedQueries);
66+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries);
6867

6968
RowMapper<?> mapper = queryMethod.isModifyingQuery() ? null : createMapper(queryMethod);
7069

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222
import org.springframework.data.jdbc.repository.query.Modifying;
2323
import org.springframework.data.jdbc.repository.query.Query;
2424
import org.springframework.data.projection.ProjectionFactory;
25+
import org.springframework.data.repository.core.NamedQueries;
2526
import org.springframework.data.repository.core.RepositoryMetadata;
2627
import org.springframework.data.repository.query.QueryMethod;
2728
import org.springframework.jdbc.core.ResultSetExtractor;
2829
import org.springframework.jdbc.core.RowMapper;
2930
import org.springframework.lang.Nullable;
3031
import org.springframework.util.StringUtils;
31-
import org.springframework.data.repository.core.NamedQueries;
3232

3333
/**
34-
* {@link QueryMethod} implementation that implements a method by executing the
35-
* query from a {@link Query} annotation on that method. Binds method arguments
36-
* to named parameters in the SQL statement.
34+
* {@link QueryMethod} implementation that implements a method by executing the query from a {@link Query} annotation on
35+
* that method. Binds method arguments to named parameters in the SQL statement.
3736
*
3837
* @author Jens Schauder
3938
* @author Kazuki Shimizu
@@ -59,49 +58,50 @@ public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFac
5958
*
6059
* @return May be {@code null}.
6160
*/
62-
6361
@Nullable
64-
public String getAnnotatedQuery() {
62+
String getDeclaredQuery() {
63+
6564
String annotatedValue = getQueryValue();
6665
return StringUtils.hasText(annotatedValue) ? annotatedValue : getNamedQuery();
6766
}
6867

6968
/**
70-
* Returns the annotated query with key value if it exists.
69+
* Returns the annotated query if it exists.
7170
*
7271
* @return May be {@code null}.
7372
*/
7473
@Nullable
75-
String getQueryValue() {
74+
private String getQueryValue() {
7675
return getMergedAnnotationAttribute("value");
7776
}
7877

79-
8078
/**
81-
* Returns the annotated query name.
79+
* Returns the named query for this method if it exists.
8280
*
8381
* @return May be {@code null}.
8482
*/
8583
@Nullable
86-
String getQueryName() {
87-
return getMergedAnnotationAttribute("name");
84+
private String getNamedQuery() {
85+
86+
String name = getQueryName();
87+
return this.namedQueries.hasQuery(name) ? this.namedQueries.getQuery(name) : null;
8888
}
89+
8990
/**
90-
* Returns the annotated query with key name if it exists.
91+
* Returns the annotated query name.
9192
*
9293
* @return May be {@code null}.
9394
*/
94-
@Nullable
95-
String getNamedQuery() {
95+
96+
private String getQueryName() {
97+
9698
String annotatedName = getMergedAnnotationAttribute("name");
97-
return (StringUtils.hasText(annotatedName) && this.namedQueries.hasQuery(annotatedName))
98-
? this.namedQueries.getQuery(annotatedName)
99-
: this.namedQueries.getQuery(super.getName());
99+
100+
return StringUtils.hasText(annotatedName) ? annotatedName : getNamedQueryName();
100101
}
101102

102103
/*
103-
* Returns the class to be used as {@link
104-
* org.springframework.jdbc.core.RowMapper}
104+
* Returns the class to be used as {@link org.springframework.jdbc.core.RowMapper}
105105
*
106106
* @return May be {@code null}.
107107
*/
@@ -111,8 +111,7 @@ Class<? extends RowMapper> getRowMapperClass() {
111111
}
112112

113113
/**
114-
* Returns the class to be used as
115-
* {@link org.springframework.jdbc.core.ResultSetExtractor}
114+
* Returns the class to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
116115
*
117116
* @return May be {@code null}.
118117
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public JdbcQueryMethod getQueryMethod() {
194194

195195
private String determineQuery() {
196196

197-
String query = queryMethod.getAnnotatedQuery();
197+
String query = queryMethod.getDeclaredQuery();
198198

199199
if (StringUtils.isEmpty(query)) {
200200
throw new IllegalStateException(String.format("No query specified on %s", queryMethod.getName()));

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import lombok.Data;
2222

23+
import java.io.IOException;
24+
import java.util.List;
25+
2326
import org.junit.ClassRule;
2427
import org.junit.Rule;
2528
import org.junit.Test;
@@ -30,27 +33,19 @@
3033
import org.springframework.context.annotation.Import;
3134
import org.springframework.core.io.ClassPathResource;
3235
import org.springframework.data.annotation.Id;
33-
import org.springframework.data.jdbc.repository.query.Query;
3436
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
35-
import org.springframework.data.jdbc.testing.QueryNamedTestConfiguration;
37+
import org.springframework.data.jdbc.testing.TestConfiguration;
3638
import org.springframework.data.repository.CrudRepository;
3739
import org.springframework.data.repository.core.NamedQueries;
3840
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
3941
import org.springframework.jdbc.core.JdbcTemplate;
4042
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
41-
import org.springframework.test.context.ActiveProfiles;
4243
import org.springframework.test.context.ContextConfiguration;
4344
import org.springframework.test.context.junit4.rules.SpringClassRule;
4445
import org.springframework.test.context.junit4.rules.SpringMethodRule;
4546
import org.springframework.test.jdbc.JdbcTestUtils;
4647
import org.springframework.transaction.annotation.Transactional;
4748

48-
import java.io.IOException;
49-
import java.util.List;
50-
51-
import javax.annotation.PostConstruct;
52-
;
53-
5449
/**
5550
* Very simple use cases for creation and usage of JdbcRepositories.
5651
*
@@ -59,14 +54,12 @@
5954
@ContextConfiguration
6055
@Transactional
6156
public class JdbcRepositoryIntegrationTests {
62-
public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT";
6357

6458
@Configuration
65-
@Import(QueryNamedTestConfiguration.class)
59+
@Import(TestConfiguration.class)
6660
static class Config {
6761

68-
@Autowired
69-
JdbcRepositoryFactory factory;
62+
@Autowired JdbcRepositoryFactory factory;
7063

7164
@Bean
7265
Class<?> testClass() {
@@ -76,7 +69,17 @@ Class<?> testClass() {
7669
@Bean
7770
DummyEntityRepository dummyEntityRepository() {
7871
return factory.getRepository(DummyEntityRepository.class);
79-
}
72+
}
73+
74+
@Bean
75+
NamedQueries namedQueries() throws IOException {
76+
77+
PropertiesFactoryBean properties = new PropertiesFactoryBean();
78+
properties.setLocation(new ClassPathResource("META-INF/jdbc-named-queries.properties"));
79+
properties.afterPropertiesSet();
80+
return new PropertiesBasedNamedQueries(properties.getObject());
81+
82+
}
8083
}
8184

8285
@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
@@ -256,10 +259,10 @@ public void findByIdReturnsEmptyWhenNoneFound() {
256259
}
257260

258261
@Test // DATAJDBC-234
259-
public void findAllQueryName() {
262+
public void findAllByQueryName() {
260263

261264
repository.save(createDummyEntity());
262-
assertThat(repository.findAllQueryName().size() > 0);
265+
assertThat(repository.findAllByNamedQuery()).hasSize(1);
263266
}
264267

265268
private static DummyEntity createDummyEntity() {
@@ -271,8 +274,7 @@ private static DummyEntity createDummyEntity() {
271274

272275
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
273276

274-
@Query(name = DUMMY_SELECT_NAME)
275-
List<DummyEntity> findAllQueryName();
277+
List<DummyEntity> findAllByNamedQuery();
276278
}
277279

278280
@Data

0 commit comments

Comments
 (0)