DATAJDBC-428 - Fixes BasicJdbcConverter readValue for immutable entity with AggregateReference field.

Original pull request: #172.
This commit is contained in:
mhyeon-lee
2019-10-04 14:39:34 +09:00
committed by Jens Schauder
parent f40c9e13c9
commit 1a3d2b0621
2 changed files with 56 additions and 0 deletions

View File

@@ -57,6 +57,7 @@ import org.springframework.util.Assert;
* @author Mark Paluch
* @author Jens Schauder
* @author Christoph Strobl
* @author Myeonghyeon Lee
* @since 1.1
* @see MappingContext
* @see SimpleTypeHolder
@@ -130,6 +131,9 @@ public class BasicJdbcConverter extends BasicRelationalConverter implements Jdbc
}
if (AggregateReference.class.isAssignableFrom(type.getType())) {
if (type.getType().isAssignableFrom(value.getClass())) {
return value;
}
return readAggregateReference(value, type);
}

View File

@@ -51,6 +51,7 @@ import org.mockito.stubbing.Answer;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.Embedded;
@@ -71,6 +72,7 @@ import org.springframework.util.Assert;
* @author Maciej Walkowiak
* @author Bastian Wilhelm
* @author Christoph Strobl
* @author Myeonghyeon Lee
*/
public class EntityRowMapperUnitTests {
@@ -129,6 +131,21 @@ public class EntityRowMapperUnitTests {
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha");
}
@Test // DATAJDBC-427
public void simpleWithReferenceGetProperlyExtracted() throws SQLException {
ResultSet rs = mockResultSet(asList("id", "name", "trivial_id"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 100L);
rs.next();
WithReference extracted = createRowMapper(WithReference.class).mapRow(rs, 1);
assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.name, e -> e.trivialId) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", AggregateReference.to(100L));
}
@Test // DATAJDBC-113
public void simpleOneToOneGetsProperlyExtracted() throws SQLException {
@@ -159,6 +176,21 @@ public class EntityRowMapperUnitTests {
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
}
@Test // DATAJDBC-427
public void immutableWithReferenceGetsProperlyExtracted() throws SQLException {
ResultSet rs = mockResultSet(asList("id", "name", "trivial_id"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 100L);
rs.next();
WithReferenceImmutable extracted = createRowMapper(WithReferenceImmutable.class).mapRow(rs, 1);
assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.name, e -> e.trivialId) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", AggregateReference.to(100L));
}
// TODO add additional test for multilevel embeddables
@Test // DATAJDBC-111
public void simpleEmbeddedGetsProperlyExtracted() throws SQLException {
@@ -440,6 +472,26 @@ public class EntityRowMapperUnitTests {
String name;
}
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Getter
static class WithReference {
@Id Long id;
String name;
AggregateReference<Trivial, Long> trivialId;
}
@Wither
@RequiredArgsConstructor
static class WithReferenceImmutable {
@Id private final Long id;
private final String name;
private final AggregateReference<TrivialImmutable, Long> trivialId;
}
static class OneToOne {
@Id Long id;