Skip to content

Commit cef4152

Browse files
mhyeon-leeschauder
authored andcommitted
DATAJDBC-428 - Fixes BasicJdbcConverter readValue for immutable entity with AggregateReference field.
Original pull request: #172.
1 parent a6b3144 commit cef4152

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Mark Paluch
5858
* @author Jens Schauder
5959
* @author Christoph Strobl
60+
* @author Myeonghyeon Lee
6061
* @since 1.1
6162
* @see MappingContext
6263
* @see SimpleTypeHolder
@@ -130,6 +131,9 @@ public Object readValue(@Nullable Object value, TypeInformation<?> type) {
130131
}
131132

132133
if (AggregateReference.class.isAssignableFrom(type.getType())) {
134+
if (type.getType().isAssignableFrom(value.getClass())) {
135+
return value;
136+
}
133137

134138
return readAggregateReference(value, type);
135139
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/EntityRowMapperUnitTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import org.springframework.data.annotation.Id;
5353
import org.springframework.data.annotation.PersistenceConstructor;
54+
import org.springframework.data.jdbc.core.mapping.AggregateReference;
5455
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
5556
import org.springframework.data.mapping.PersistentPropertyPath;
5657
import org.springframework.data.relational.core.mapping.Embedded;
@@ -71,6 +72,7 @@
7172
* @author Maciej Walkowiak
7273
* @author Bastian Wilhelm
7374
* @author Christoph Strobl
75+
* @author Myeonghyeon Lee
7476
*/
7577
public class EntityRowMapperUnitTests {
7678

@@ -129,6 +131,21 @@ public void namingStrategyGetsHonoredForConstructor() throws SQLException {
129131
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha");
130132
}
131133

134+
@Test // DATAJDBC-427
135+
public void simpleWithReferenceGetProperlyExtracted() throws SQLException {
136+
137+
ResultSet rs = mockResultSet(asList("id", "name", "trivial_id"), //
138+
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 100L);
139+
rs.next();
140+
141+
WithReference extracted = createRowMapper(WithReference.class).mapRow(rs, 1);
142+
143+
assertThat(extracted) //
144+
.isNotNull() //
145+
.extracting(e -> e.id, e -> e.name, e -> e.trivialId) //
146+
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", AggregateReference.to(100L));
147+
}
148+
132149
@Test // DATAJDBC-113
133150
public void simpleOneToOneGetsProperlyExtracted() throws SQLException {
134151

@@ -159,6 +176,21 @@ public void immutableOneToOneGetsProperlyExtracted() throws SQLException {
159176
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
160177
}
161178

179+
@Test // DATAJDBC-427
180+
public void immutableWithReferenceGetsProperlyExtracted() throws SQLException {
181+
182+
ResultSet rs = mockResultSet(asList("id", "name", "trivial_id"), //
183+
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 100L);
184+
rs.next();
185+
186+
WithReferenceImmutable extracted = createRowMapper(WithReferenceImmutable.class).mapRow(rs, 1);
187+
188+
assertThat(extracted) //
189+
.isNotNull() //
190+
.extracting(e -> e.id, e -> e.name, e -> e.trivialId) //
191+
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", AggregateReference.to(100L));
192+
}
193+
162194
// TODO add additional test for multilevel embeddables
163195
@Test // DATAJDBC-111
164196
public void simpleEmbeddedGetsProperlyExtracted() throws SQLException {
@@ -440,6 +472,26 @@ static class Trivial {
440472
String name;
441473
}
442474

475+
@EqualsAndHashCode
476+
@NoArgsConstructor
477+
@AllArgsConstructor
478+
@Getter
479+
static class WithReference {
480+
481+
@Id Long id;
482+
String name;
483+
AggregateReference<Trivial, Long> trivialId;
484+
}
485+
486+
@Wither
487+
@RequiredArgsConstructor
488+
static class WithReferenceImmutable {
489+
490+
@Id private final Long id;
491+
private final String name;
492+
private final AggregateReference<TrivialImmutable, Long> trivialId;
493+
}
494+
443495
static class OneToOne {
444496

445497
@Id Long id;

0 commit comments

Comments
 (0)