Skip to content

Commit 63f2331

Browse files
committed
Avoid conversion when return value is null.
Closes #1167
1 parent 9554fdf commit 63f2331

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
184184

185185
T object = delegate.mapRow(rs, rowNum);
186186

187-
return converter.convert(object);
187+
return object == null ? null : converter.convert(object);
188188
}
189189
}
190190
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.junit.jupiter.api.BeforeEach;
3838
import org.junit.jupiter.api.Test;
3939
import org.junit.jupiter.api.extension.ExtendWith;
40-
4140
import org.springframework.beans.factory.annotation.Autowired;
4241
import org.springframework.beans.factory.config.PropertiesFactoryBean;
4342
import org.springframework.context.ApplicationListener;
@@ -70,6 +69,7 @@
7069
import org.springframework.jdbc.core.JdbcTemplate;
7170
import org.springframework.jdbc.core.RowMapper;
7271
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
72+
import org.springframework.lang.Nullable;
7373
import org.springframework.test.context.TestExecutionListeners;
7474
import org.springframework.test.context.junit.jupiter.SpringExtension;
7575
import org.springframework.test.jdbc.JdbcTestUtils;
@@ -559,6 +559,22 @@ void queryByAggregateReference() {
559559
assertThat(result).extracting(e -> e.idProp).containsExactly(two.idProp);
560560
}
561561

562+
@Test // GH-1167
563+
void stringResult() {
564+
565+
repository.save(createDummyEntity()); // just ensure we have data in the table
566+
567+
assertThat(repository.returnInput("HELLO")).isEqualTo("HELLO");
568+
}
569+
570+
@Test // GH-1167
571+
void nullStringResult() {
572+
573+
repository.save(createDummyEntity()); // just ensure we have data in the table
574+
575+
assertThat(repository.returnInput(null)).isNull();
576+
}
577+
562578
private Instant createDummyBeforeAndAfterNow() {
563579

564580
Instant now = Instant.now();
@@ -640,6 +656,10 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
640656
List<DummyEntity> findByRef(int ref);
641657

642658
List<DummyEntity> findByRef(AggregateReference<DummyEntity, Long> ref);
659+
660+
@Query("SELECT CAST(:hello AS CHAR(5)) FROM DUMMY_ENTITY")
661+
@Nullable
662+
String returnInput(@Nullable String hello);
643663
}
644664

645665
@Configuration

0 commit comments

Comments
 (0)