Skip to content

Commit 2c0a5b7

Browse files
maciejwalkowiakschauder
authored andcommitted
DATAJDBC-286 - Fixes one-to-one relationships for immutable entities.
Original pull request: #99.
1 parent 7e0cafe commit 2c0a5b7

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Jens Schauder
3939
* @author Oliver Gierke
4040
* @author Mark Paluch
41+
* @author Maciej Walkowiak
4142
*/
4243
public class EntityRowMapper<T> implements RowMapper<T> {
4344

@@ -76,7 +77,7 @@ public T mapRow(ResultSet resultSet, int rowNumber) {
7677
idValue = readFrom(resultSet, idProperty, prefix);
7778
}
7879

79-
T result = createInstance(entity, resultSet, idValue);
80+
T result = createInstance(entity, resultSet, idValue, prefix);
8081

8182
return entity.requiresPropertyPopulation() //
8283
? populateProperties(result, resultSet) //
@@ -97,21 +98,21 @@ private T populateProperties(T result, ResultSet resultSet) {
9798
continue;
9899
}
99100

100-
propertyAccessor.setProperty(property, readOrLoadProperty(resultSet, id, property));
101+
propertyAccessor.setProperty(property, readOrLoadProperty(resultSet, id, property, ""));
101102
}
102103

103104
return propertyAccessor.getBean();
104105
}
105106

106107
@Nullable
107-
private Object readOrLoadProperty(ResultSet resultSet, @Nullable Object id, RelationalPersistentProperty property) {
108+
private Object readOrLoadProperty(ResultSet resultSet, @Nullable Object id, RelationalPersistentProperty property, String prefix) {
108109

109110
if (property.isCollectionLike() && id != null) {
110111
return accessStrategy.findAllByProperty(id, property);
111112
} else if (property.isMap() && id != null) {
112113
return ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(accessStrategy.findAllByProperty(id, property));
113114
} else {
114-
return readFrom(resultSet, property, "");
115+
return readFrom(resultSet, property, prefix);
115116
}
116117
}
117118

@@ -159,7 +160,7 @@ private <S> S readEntityFrom(ResultSet rs, RelationalPersistentProperty property
159160
return null;
160161
}
161162

162-
S instance = createInstance(entity, rs, idValue);
163+
S instance = createInstance(entity, rs, idValue, prefix);
163164

164165
PersistentPropertyAccessor<S> accessor = converter.getPropertyAccessor(entity, instance);
165166

@@ -180,7 +181,7 @@ private Object getObjectFromResultSet(ResultSet rs, String backreferenceName) {
180181
}
181182
}
182183

183-
private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, @Nullable Object idValue) {
184+
private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, @Nullable Object idValue, String prefix) {
184185

185186
return converter.createInstance(entity, parameter -> {
186187

@@ -190,7 +191,7 @@ private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs,
190191

191192
RelationalPersistentProperty property = entity.getRequiredPersistentProperty(parameterName);
192193

193-
return readOrLoadProperty(rs, idValue, property);
194+
return readOrLoadProperty(rs, idValue, property, prefix);
194195
});
195196
}
196197
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
*
5959
* @author Jens Schauder
6060
* @author Mark Paluch
61+
* @author Maciej Walkowiak
6162
*/
6263
public class EntityRowMapperUnitTests {
6364

@@ -131,6 +132,21 @@ public void simpleOneToOneGetsProperlyExtracted() throws SQLException {
131132
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
132133
}
133134

135+
@Test // DATAJDBC-286
136+
public void immutableOneToOneGetsProperlyExtracted() throws SQLException {
137+
138+
ResultSet rs = mockResultSet(asList("id", "name", "child_id", "child_name"), //
139+
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
140+
rs.next();
141+
142+
OneToOneImmutable extracted = createRowMapper(OneToOneImmutable.class).mapRow(rs, 1);
143+
144+
assertThat(extracted) //
145+
.isNotNull() //
146+
.extracting(e -> e.id, e -> e.name, e -> e.child.id, e -> e.child.name) //
147+
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha", 24L, "beta");
148+
}
149+
134150
@Test // DATAJDBC-113
135151
public void collectionReferenceGetsLoadedWithAdditionalSelect() throws SQLException {
136152

@@ -371,6 +387,15 @@ static class OneToOne {
371387
Trivial child;
372388
}
373389

390+
@RequiredArgsConstructor
391+
@Wither
392+
static class OneToOneImmutable {
393+
394+
private final @Id Long id;
395+
private final String name;
396+
private final TrivialImmutable child;
397+
}
398+
374399
static class OneToSet {
375400

376401
@Id Long id;

0 commit comments

Comments
 (0)