@@ -67,7 +67,16 @@ public EntityRowMapper(RelationalPersistentEntity<T> entity, RelationalMappingCo
67
67
@ Override
68
68
public T mapRow (ResultSet resultSet , int rowNumber ) {
69
69
70
- T result = createInstance (entity , resultSet , "" );
70
+ String prefix = "" ;
71
+
72
+ RelationalPersistentProperty idProperty = entity .getIdProperty ();
73
+
74
+ Object idValue = null ;
75
+ if (idProperty != null ) {
76
+ idValue = readFrom (resultSet , idProperty , prefix );
77
+ }
78
+
79
+ T result = createInstance (entity , resultSet , idValue );
71
80
72
81
return entity .requiresPropertyPopulation () //
73
82
? populateProperties (result , resultSet ) //
@@ -88,26 +97,24 @@ private T populateProperties(T result, ResultSet resultSet) {
88
97
continue ;
89
98
}
90
99
91
- if (property .isCollectionLike () && id != null ) {
92
-
93
- propertyAccessor .setProperty (property , accessStrategy .findAllByProperty (id , property ));
94
-
95
- } else if (property .isMap () && id != null ) {
96
-
97
- Iterable <Object > allByProperty = accessStrategy .findAllByProperty (id , property );
98
- propertyAccessor .setProperty (property , ITERABLE_OF_ENTRY_TO_MAP_CONVERTER .convert (allByProperty ));
99
-
100
- } else {
101
-
102
- final Object value = readFrom (resultSet , property , "" );
103
-
104
- propertyAccessor .setProperty (property , value );
105
- }
100
+ propertyAccessor .setProperty (property , readOrLoadProperty (resultSet , id , property ));
106
101
}
107
102
108
103
return propertyAccessor .getBean ();
109
104
}
110
105
106
+ @ Nullable
107
+ private Object readOrLoadProperty (ResultSet resultSet , @ Nullable Object id , RelationalPersistentProperty property ) {
108
+
109
+ if (property .isCollectionLike () && id != null ) {
110
+ return accessStrategy .findAllByProperty (id , property );
111
+ } else if (property .isMap () && id != null ) {
112
+ return ITERABLE_OF_ENTRY_TO_MAP_CONVERTER .convert (accessStrategy .findAllByProperty (id , property ));
113
+ } else {
114
+ return readFrom (resultSet , property , "" );
115
+ }
116
+ }
117
+
111
118
/**
112
119
* Read a single value or a complete Entity from the {@link ResultSet} passed as an argument.
113
120
*
@@ -139,14 +146,20 @@ private <S> S readEntityFrom(ResultSet rs, RelationalPersistentProperty property
139
146
140
147
RelationalPersistentProperty idProperty = entity .getIdProperty ();
141
148
149
+ Object idValue = null ;
150
+
151
+ if (idProperty != null ) {
152
+ idValue = readFrom (rs , idProperty , prefix );
153
+ }
154
+
142
155
if ((idProperty != null //
143
- ? readFrom ( rs , idProperty , prefix ) //
156
+ ? idValue //
144
157
: getObjectFromResultSet (rs , prefix + property .getReverseColumnName ()) //
145
158
) == null ) {
146
159
return null ;
147
160
}
148
161
149
- S instance = createInstance (entity , rs , prefix );
162
+ S instance = createInstance (entity , rs , idValue );
150
163
151
164
PersistentPropertyAccessor <S > accessor = converter .getPropertyAccessor (entity , instance );
152
165
@@ -167,7 +180,7 @@ private Object getObjectFromResultSet(ResultSet rs, String backreferenceName) {
167
180
}
168
181
}
169
182
170
- private <S > S createInstance (RelationalPersistentEntity <S > entity , ResultSet rs , String prefix ) {
183
+ private <S > S createInstance (RelationalPersistentEntity <S > entity , ResultSet rs , @ Nullable Object idValue ) {
171
184
172
185
return converter .createInstance (entity , parameter -> {
173
186
@@ -176,7 +189,8 @@ private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs,
176
189
Assert .notNull (parameterName , "A constructor parameter name must not be null to be used with Spring Data JDBC" );
177
190
178
191
RelationalPersistentProperty property = entity .getRequiredPersistentProperty (parameterName );
179
- return readFrom (rs , property , prefix );
192
+
193
+ return readOrLoadProperty (rs , idValue , property );
180
194
});
181
195
}
182
196
}
0 commit comments