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:
Jens Schauder
2021-08-10 15:05:14 +02:00
committed by Mark Paluch
parent 18e4e5a67a
commit 27f07a33d4
3 changed files with 15 additions and 24 deletions

View File

@@ -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() //

View File

@@ -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);

View File

@@ -1 +1,2 @@
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
DummyEntity.customQuery=SELECT * FROM DUMMY_ENTITY