Browse Source

DATAJDBC-529 - Use specialized ResultSetExtractor for exists queries.

Exists queries do not look at the value returned in the ResultSet but just check if there is a row.

Original pull request: #212.
pull/216/head
Jens Schauder 6 years ago committed by Mark Paluch
parent
commit
370ee4464e
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 4
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java
  2. 7
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java
  3. 20
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

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

@ -80,10 +80,10 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery { @@ -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<Object> extractor, RowMapper<Object> rowMapper) {
@Nullable ResultSetExtractor<?> extractor, RowMapper<?> rowMapper) {
if (queryMethod.isModifyingQuery()) {
return createModifyingQueryExecutor();

7
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 @@ -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 { @@ -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<Boolean> extractor = tree.isExistsProjection() ? (ResultSet::next) : null;
this.execution = getQueryExecution(queryMethod, extractor, rowMapper);
}
private Sort getDynamicSort(RelationalParameterAccessor accessor) {

20
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -26,11 +26,11 @@ import java.time.Instant; @@ -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 { @@ -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 { @@ -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

Loading…
Cancel
Save