Skip to content

Fixes annotated named queries. #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,16 @@ private String getQueryValue() {
@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();
}

/**
Expand All @@ -174,7 +169,6 @@ Class<? extends RowMapper> getRowMapperClass() {
return getMergedAnnotationAttribute("rowMapperClass");
}


/**
* Returns the name of the bean to be used as {@link org.springframework.jdbc.core.RowMapper}
*
Expand Down Expand Up @@ -225,24 +219,11 @@ private <T> T getMergedAnnotationAttribute(String attribute) {

/**
* 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() //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ public void findAllByQueryName() {
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() {

Expand Down Expand Up @@ -567,6 +574,8 @@ private Instant createDummyBeforeAndAfterNow() {
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {

List<DummyEntity> findAllByNamedQuery();
@Query(name = "DummyEntity.customQuery")
List<DummyEntity> findAllByCustomNamedQuery();

List<DummyEntity> findAllByPointInTimeAfter(Instant instant);

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
DummyEntity.customQuery=SELECT * FROM DUMMY_ENTITY
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-1022-named-queries-broken-SNAPSHOT</version>
</parent>

<properties>
Expand Down