diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java index c15b52b99..23a50e848 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java @@ -80,10 +80,10 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery { * @param queryMethod must not be {@literal null}. * @param extractor must not be {@literal null}. * @param rowMapper must not be {@literal null}. - * @return + * @return a JdbcQueryExecution appropriate for {@literal queryMethod}. Guaranteed to be not {@literal null}. */ protected JdbcQueryExecution getQueryExecution(JdbcQueryMethod queryMethod, - @Nullable ResultSetExtractor extractor, RowMapper rowMapper) { + @Nullable ResultSetExtractor extractor, RowMapper rowMapper) { if (queryMethod.isModifyingQuery()) { return createModifyingQueryExecutor(); diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java index 27fa0981f..c9bda8d00 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java @@ -24,10 +24,13 @@ import org.springframework.data.relational.repository.query.RelationalParameterA import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor; import org.springframework.data.repository.query.Parameters; import org.springframework.data.repository.query.parser.PartTree; +import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.util.Assert; +import java.sql.ResultSet; + /** * An {@link AbstractJdbcQuery} implementation based on a {@link PartTree}. * @@ -72,7 +75,9 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery { this.tree = new PartTree(queryMethod.getName(), queryMethod.getEntityInformation().getJavaType()); JdbcQueryCreator.validate(this.tree, this.parameters, this.converter.getMappingContext()); - this.execution = getQueryExecution(queryMethod, null, rowMapper); + ResultSetExtractor extractor = tree.isExistsProjection() ? (ResultSet::next) : null; + + this.execution = getQueryExecution(queryMethod, extractor, rowMapper); } private Sort getDynamicSort(RelationalParameterAccessor accessor) { diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java index 392d42af4..100f00158 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java @@ -26,11 +26,11 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; +import org.assertj.core.api.SoftAssertions; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.ApplicationListener; @@ -355,6 +355,22 @@ public class JdbcRepositoryIntegrationTests { assertThat(loaded.pointInTime).isNull(); } + @Test // DATAJDBC-529 + public void existsWorksAsExpected() { + + DummyEntity dummy = repository.save(createDummyEntity()); + + SoftAssertions.assertSoftly(softly -> { + + softly.assertThat(repository.existsByName(dummy.getName())) // + .describedAs("Positive") // + .isTrue(); + softly.assertThat(repository.existsByName("not an existing name")) // + .describedAs("Positive") // + .isFalse(); + }); + } + private static DummyEntity createDummyEntity() { DummyEntity entity = new DummyEntity(); @@ -380,6 +396,8 @@ public class JdbcRepositoryIntegrationTests { @Query("SELECT id_Prop from dummy_entity where id_Prop = :id") DummyEntity withMissingColumn(@Param("id") Long id); + + boolean existsByName(String name); } @Data