DATAJDBC-286 - Fixes one-to-one relationships for immutable entities.
Original pull request: #99.
This commit is contained in:
committed by
Jens Schauder
parent
7e0cafebf2
commit
2c0a5b7e2e
@@ -38,6 +38,7 @@ import org.springframework.util.Assert;
|
||||
* @author Jens Schauder
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Maciej Walkowiak
|
||||
*/
|
||||
public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
@@ -76,7 +77,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
idValue = readFrom(resultSet, idProperty, prefix);
|
||||
}
|
||||
|
||||
T result = createInstance(entity, resultSet, idValue);
|
||||
T result = createInstance(entity, resultSet, idValue, prefix);
|
||||
|
||||
return entity.requiresPropertyPopulation() //
|
||||
? populateProperties(result, resultSet) //
|
||||
@@ -97,21 +98,21 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
continue;
|
||||
}
|
||||
|
||||
propertyAccessor.setProperty(property, readOrLoadProperty(resultSet, id, property));
|
||||
propertyAccessor.setProperty(property, readOrLoadProperty(resultSet, id, property, ""));
|
||||
}
|
||||
|
||||
return propertyAccessor.getBean();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Object readOrLoadProperty(ResultSet resultSet, @Nullable Object id, RelationalPersistentProperty property) {
|
||||
private Object readOrLoadProperty(ResultSet resultSet, @Nullable Object id, RelationalPersistentProperty property, String prefix) {
|
||||
|
||||
if (property.isCollectionLike() && id != null) {
|
||||
return accessStrategy.findAllByProperty(id, property);
|
||||
} else if (property.isMap() && id != null) {
|
||||
return ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(accessStrategy.findAllByProperty(id, property));
|
||||
} else {
|
||||
return readFrom(resultSet, property, "");
|
||||
return readFrom(resultSet, property, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +160,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
return null;
|
||||
}
|
||||
|
||||
S instance = createInstance(entity, rs, idValue);
|
||||
S instance = createInstance(entity, rs, idValue, prefix);
|
||||
|
||||
PersistentPropertyAccessor<S> accessor = converter.getPropertyAccessor(entity, instance);
|
||||
|
||||
@@ -180,7 +181,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, @Nullable Object idValue) {
|
||||
private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, @Nullable Object idValue, String prefix) {
|
||||
|
||||
return converter.createInstance(entity, parameter -> {
|
||||
|
||||
@@ -190,7 +191,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
RelationalPersistentProperty property = entity.getRequiredPersistentProperty(parameterName);
|
||||
|
||||
return readOrLoadProperty(rs, idValue, property);
|
||||
return readOrLoadProperty(rs, idValue, property, prefix);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
* @author Maciej Walkowiak
|
||||
*/
|
||||
public class EntityRowMapperUnitTests {
|
||||
|
||||
@@ -131,6 +132,21 @@ public class EntityRowMapperUnitTests {
|
||||
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-286
|
||||
public void immutableOneToOneGetsProperlyExtracted() throws SQLException {
|
||||
|
||||
ResultSet rs = mockResultSet(asList("id", "name", "child_id", "child_name"), //
|
||||
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
|
||||
rs.next();
|
||||
|
||||
OneToOneImmutable extracted = createRowMapper(OneToOneImmutable.class).mapRow(rs, 1);
|
||||
|
||||
assertThat(extracted) //
|
||||
.isNotNull() //
|
||||
.extracting(e -> e.id, e -> e.name, e -> e.child.id, e -> e.child.name) //
|
||||
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-113
|
||||
public void collectionReferenceGetsLoadedWithAdditionalSelect() throws SQLException {
|
||||
|
||||
@@ -371,6 +387,15 @@ public class EntityRowMapperUnitTests {
|
||||
Trivial child;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Wither
|
||||
static class OneToOneImmutable {
|
||||
|
||||
private final @Id Long id;
|
||||
private final String name;
|
||||
private final TrivialImmutable child;
|
||||
}
|
||||
|
||||
static class OneToSet {
|
||||
|
||||
@Id Long id;
|
||||
|
||||
Reference in New Issue
Block a user