Skip to content

Commit 7f30a9f

Browse files
the name parameter has been created for Query.class.The NamedQuery interface is used to extract from the properties file called jdbc-named-queries.properties.Test jUnit created. modified: spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java, modified: spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java, modified: spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java , modified: spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java
1 parent 7a738ee commit 7f30a9f

File tree

4 files changed

+130
-19
lines changed

4 files changed

+130
-19
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* parameters will get bound to the arguments of the annotated method.
3232
*
3333
* @author Jens Schauder
34+
* @author Moises Cisneros
3435
*/
3536
@Retention(RetentionPolicy.RUNTIME)
3637
@Target(ElementType.METHOD)
@@ -41,8 +42,14 @@
4142
/**
4243
* The SQL statement to execute when the annotated method gets invoked.
4344
*/
44-
String value();
45+
String value() default "";
4546

47+
/**
48+
* The named query to be used. If not defined, the name of
49+
* {@code $ domainClass}.${queryMethodName}} will be used.
50+
*/
51+
String name() default "";
52+
4653
/**
4754
* Optional {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
4855
* along with {@link #resultSetExtractorClass()} only one of the two can be set.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
8888
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
8989
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
9090

91-
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory);
91+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory,namedQueries);
9292

9393
RowMapper<?> mapper = queryMethod.isModifyingQuery() ? null : createMapper(queryMethod);
9494

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import org.springframework.jdbc.core.ResultSetExtractor;
2828
import org.springframework.jdbc.core.RowMapper;
2929
import org.springframework.lang.Nullable;
30+
import org.springframework.util.StringUtils;
31+
import org.springframework.data.repository.core.NamedQueries;
3032

3133
/**
32-
* {@link QueryMethod} implementation that implements a method by executing the query from a {@link Query} annotation on
33-
* that method. Binds method arguments to named parameters in the SQL statement.
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.
3437
*
3538
* @author Jens Schauder
3639
* @author Kazuki Shimizu
@@ -40,11 +43,13 @@
4043
public class JdbcQueryMethod extends QueryMethod {
4144

4245
private final Method method;
46+
private final NamedQueries namedQueries;
4347

44-
public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
48+
public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
49+
NamedQueries namedQueries) {
4550

4651
super(method, metadata, factory);
47-
52+
this.namedQueries = namedQueries;
4853
this.method = method;
4954
}
5055

@@ -53,13 +58,38 @@ public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFac
5358
*
5459
* @return May be {@code null}.
5560
*/
61+
5662
@Nullable
5763
String getAnnotatedQuery() {
58-
return getMergedAnnotationAttribute("value");
64+
String annotatedValue = getNamedQueryvalue();
65+
return StringUtils.hasText(annotatedValue) ? annotatedValue : getNamedQueryName();
5966
}
6067

6168
/**
62-
* Returns the class to be used as {@link org.springframework.jdbc.core.RowMapper}
69+
* Returns the annotated query with key value if it exists.
70+
*
71+
* @return May be {@code null}.
72+
*/
73+
@Nullable
74+
public String getNamedQueryvalue() {
75+
return getMergedAnnotationAttribute("value");
76+
}
77+
/**
78+
* Returns the annotated query with key name if it exists.
79+
*
80+
* @return May be {@code null}.
81+
*/
82+
@Nullable
83+
public String getNamedQueryName() {
84+
String annotatedName = getMergedAnnotationAttribute("name");
85+
return (StringUtils.hasText(annotatedName) && this.namedQueries.hasQuery(annotatedName))
86+
? this.namedQueries.getQuery(annotatedName)
87+
: super.getNamedQueryName();
88+
}
89+
90+
/*
91+
* Returns the class to be used as {@link
92+
* org.springframework.jdbc.core.RowMapper}
6393
*
6494
* @return May be {@code null}.
6595
*/
@@ -69,7 +99,8 @@ Class<? extends RowMapper> getRowMapperClass() {
6999
}
70100

71101
/**
72-
* Returns the class to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
102+
* Returns the class to be used as
103+
* {@link org.springframework.jdbc.core.ResultSetExtractor}
73104
*
74105
* @return May be {@code null}.
75106
*/

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
import java.lang.reflect.Method;
2323
import java.sql.ResultSet;
24+
import java.util.Properties;
2425

2526
import org.junit.Test;
2627
import org.springframework.data.jdbc.repository.query.Query;
2728
import org.springframework.data.projection.ProjectionFactory;
29+
import org.springframework.data.repository.core.NamedQueries;
2830
import org.springframework.data.repository.core.RepositoryMetadata;
31+
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
2932
import org.springframework.jdbc.core.RowMapper;
3033

3134
/**
@@ -36,19 +39,24 @@
3639
*/
3740
public class JdbcQueryMethodUnitTests {
3841

39-
public static final String DUMMY_SELECT = "SELECT something";
42+
public static final String DUMMY_SELECT_VALUE = "SELECT something";
43+
public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT";
44+
public static final String DUMMY_SELECT_NAME_VALUE= "SELECT something NAME AND VALUE";
4045

4146
@Test // DATAJDBC-165
4247
public void returnsSqlStatement() throws NoSuchMethodException {
4348

4449
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
4550

4651
doReturn(String.class).when(metadata).getReturnedDomainClass(any(Method.class));
47-
48-
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"),
49-
metadata, mock(ProjectionFactory.class));
50-
51-
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT);
52+
Properties properties = new Properties();
53+
properties.setProperty(DUMMY_SELECT_NAME, DUMMY_SELECT_VALUE);
54+
NamedQueries nameQueries = new PropertiesBasedNamedQueries(properties);
55+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(
56+
JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"), metadata,
57+
mock(ProjectionFactory.class), nameQueries);
58+
59+
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT_VALUE);
5260
}
5361

5462
@Test // DATAJDBC-165
@@ -57,15 +65,80 @@ public void returnsSpecifiedRowMapperClass() throws NoSuchMethodException {
5765
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
5866

5967
doReturn(String.class).when(metadata).getReturnedDomainClass(any(Method.class));
68+
Properties properties = new Properties();
69+
properties.setProperty(DUMMY_SELECT_NAME, DUMMY_SELECT_VALUE);
70+
NamedQueries nameQueries = new PropertiesBasedNamedQueries(properties);
6071

61-
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"),
62-
metadata, mock(ProjectionFactory.class));
72+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(
73+
JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"), metadata,
74+
mock(ProjectionFactory.class), nameQueries);
6375

6476
assertThat(queryMethod.getRowMapperClass()).isEqualTo(CustomRowMapper.class);
6577
}
6678

67-
@Query(value = DUMMY_SELECT, rowMapperClass = CustomRowMapper.class)
68-
private void queryMethod() {}
79+
@Test // DATAJDBC-234
80+
public void returnsSqlStatementName() throws NoSuchMethodException {
81+
82+
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
83+
84+
doReturn(String.class).when(metadata).getReturnedDomainClass(any(Method.class));
85+
86+
Properties properties = new Properties();
87+
properties.setProperty(DUMMY_SELECT_NAME, DUMMY_SELECT_VALUE);
88+
NamedQueries nameQueries = new PropertiesBasedNamedQueries(properties);
89+
90+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(
91+
JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethodName"), metadata,
92+
mock(ProjectionFactory.class), nameQueries);
93+
94+
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT_VALUE);
95+
}
96+
@Test // DATAJDBC-234
97+
public void returnsSqlStatementNameAndValue() throws NoSuchMethodException {
98+
99+
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
100+
101+
doReturn(String.class).when(metadata).getReturnedDomainClass(any(Method.class));
102+
103+
Properties properties = new Properties();
104+
properties.setProperty(DUMMY_SELECT_NAME, DUMMY_SELECT_VALUE);
105+
NamedQueries nameQueries = new PropertiesBasedNamedQueries(properties);
106+
107+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(
108+
JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethodNameAndValue"), metadata,
109+
mock(ProjectionFactory.class), nameQueries);
110+
111+
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT_NAME_VALUE);
112+
}
113+
114+
@Test // DATAJDBC-234
115+
public void returnsSpecifiedRowMapperClassName() throws NoSuchMethodException {
116+
117+
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
118+
Properties properties = new Properties();
119+
properties.setProperty(DUMMY_SELECT_NAME, DUMMY_SELECT_VALUE);
120+
NamedQueries nameQueries = new PropertiesBasedNamedQueries(properties);
121+
122+
doReturn(String.class).when(metadata).getReturnedDomainClass(any(Method.class));
123+
124+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(
125+
JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethodName"), metadata,
126+
mock(ProjectionFactory.class), nameQueries);
127+
128+
assertThat(queryMethod.getRowMapperClass()).isEqualTo(CustomRowMapper.class);
129+
}
130+
131+
@Query(value = DUMMY_SELECT_VALUE, rowMapperClass = CustomRowMapper.class)
132+
private void queryMethod() {
133+
}
134+
135+
@Query(name = DUMMY_SELECT_NAME, rowMapperClass = CustomRowMapper.class)
136+
private void queryMethodName() {
137+
}
138+
139+
@Query(value = DUMMY_SELECT_NAME_VALUE, name = DUMMY_SELECT_NAME, rowMapperClass = CustomRowMapper.class)
140+
private void queryMethodNameAndValue() {
141+
}
69142

70143
private class CustomRowMapper implements RowMapper<Object> {
71144

0 commit comments

Comments
 (0)