Fixes annotated named queries.
@Query.name wasn't considered when trying to find a named query for a query method. Closes #1022 Original pull request: #1039.
This commit is contained in:
committed by
Mark Paluch
parent
18e4e5a67a
commit
27f07a33d4
@@ -147,21 +147,16 @@ public class JdbcQueryMethod extends QueryMethod {
|
||||
@Nullable
|
||||
private String getNamedQuery() {
|
||||
|
||||
String name = getQueryName();
|
||||
String name = getNamedQueryName();
|
||||
return this.namedQueries.hasQuery(name) ? this.namedQueries.getQuery(name) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the annotated query name.
|
||||
*
|
||||
* @return May be {@code null}.
|
||||
*/
|
||||
|
||||
private String getQueryName() {
|
||||
@Override
|
||||
public String getNamedQueryName() {
|
||||
|
||||
String annotatedName = getMergedAnnotationAttribute("name");
|
||||
|
||||
return StringUtils.hasText(annotatedName) ? annotatedName : getNamedQueryName();
|
||||
return StringUtils.hasText(annotatedName) ? annotatedName : super.getNamedQueryName();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,7 +169,6 @@ public class JdbcQueryMethod extends QueryMethod {
|
||||
return getMergedAnnotationAttribute("rowMapperClass");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the bean to be used as {@link org.springframework.jdbc.core.RowMapper}
|
||||
*
|
||||
@@ -225,24 +219,11 @@ public class JdbcQueryMethod extends QueryMethod {
|
||||
|
||||
/**
|
||||
* Returns whether the method has an annotated query.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean hasAnnotatedQuery() {
|
||||
return findAnnotatedQuery().isPresent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the query string declared in a {@link Query} annotation or {@literal null} if neither the annotation found
|
||||
* nor the attribute was specified.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
String getAnnotatedQuery() {
|
||||
return findAnnotatedQuery().orElse(null);
|
||||
}
|
||||
|
||||
private Optional<String> findAnnotatedQuery() {
|
||||
|
||||
return lookupQueryAnnotation() //
|
||||
|
||||
@@ -311,6 +311,13 @@ public class JdbcRepositoryIntegrationTests {
|
||||
assertThat(repository.findAllByNamedQuery()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // GH-1022
|
||||
public void findAllByCustomQueryName() {
|
||||
|
||||
repository.save(createDummyEntity());
|
||||
assertThat(repository.findAllByCustomNamedQuery()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-341
|
||||
public void findWithMissingQuery() {
|
||||
|
||||
@@ -393,6 +400,8 @@ public class JdbcRepositoryIntegrationTests {
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
|
||||
|
||||
List<DummyEntity> findAllByNamedQuery();
|
||||
@Query(name = "DummyEntity.customQuery")
|
||||
List<DummyEntity> findAllByCustomNamedQuery();
|
||||
|
||||
List<DummyEntity> findAllByPointInTimeAfter(Instant instant);
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
|
||||
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
|
||||
DummyEntity.customQuery=SELECT * FROM DUMMY_ENTITY
|
||||
Reference in New Issue
Block a user