Skip to content

Commit 463d9c3

Browse files
committed
Avoid conversion when return value is null.
Closes #1167
1 parent f95db3e commit 463d9c3

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;
@@ -68,6 +67,7 @@
6867
import org.springframework.jdbc.core.JdbcTemplate;
6968
import org.springframework.jdbc.core.RowMapper;
7069
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
70+
import org.springframework.lang.Nullable;
7171
import org.springframework.test.context.TestExecutionListeners;
7272
import org.springframework.test.context.junit.jupiter.SpringExtension;
7373
import org.springframework.test.jdbc.JdbcTestUtils;
@@ -549,6 +549,22 @@ void queryByAggregateReference() {
549549
assertThat(result).extracting(e -> e.idProp).containsExactly(two.idProp);
550550
}
551551

552+
@Test // GH-1167
553+
void stringResult() {
554+
555+
repository.save(createDummyEntity()); // just ensure we have data in the table
556+
557+
assertThat(repository.returnInput("HELLO")).isEqualTo("HELLO");
558+
}
559+
560+
@Test // GH-1167
561+
void nullStringResult() {
562+
563+
repository.save(createDummyEntity()); // just ensure we have data in the table
564+
565+
assertThat(repository.returnInput(null)).isNull();
566+
}
567+
552568
private Instant createDummyBeforeAndAfterNow() {
553569

554570
Instant now = Instant.now();
@@ -625,6 +641,10 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
625641

626642
List<DummyEntity> findByRef(int ref);
627643
List<DummyEntity> findByRef(AggregateReference<DummyEntity, Long> ref);
644+
645+
@Query("SELECT CAST(:hello AS CHAR(5)) FROM DUMMY_ENTITY")
646+
@Nullable
647+
String returnInput(@Nullable String hello);
628648
}
629649

630650
@Configuration

0 commit comments

Comments
 (0)