1919import java .util .ArrayList ;
2020import java .util .List ;
2121import java .util .Map ;
22+ import java .util .function .Function ;
2223import java .util .function .Predicate ;
2324
2425import org .springframework .data .jdbc .core .mapping .JdbcValue ;
@@ -60,21 +61,19 @@ public SqlParametersFactory(RelationalMappingContext context, JdbcConverter conv
6061 /**
6162 * Creates the parameters for a SQL insert operation.
6263 *
63- * @param instance the entity to be inserted. Must not be {@code null}.
64- * @param domainType the type of the instance. Must not be {@code null}.
65- * @param identifier information about data that needs to be considered for the insert but which is not part of the
66- * entity. Namely references back to a parent entity and key/index columns for entities that are stored in a
67- * {@link Map} or {@link List}.
64+ * @param instance the entity to be inserted. Must not be {@code null}.
65+ * @param domainType the type of the instance. Must not be {@code null}.
66+ * @param identifier information about data that needs to be considered for the insert but which is not part of the
67+ * entity. Namely references back to a parent entity and key/index columns for entities that are stored in a
68+ * {@link Map} or {@link List}.
6869 * @param idValueSource the {@link IdValueSource} for the insert.
6970 * @return the {@link SqlIdentifierParameterSource} for the insert. Guaranteed to not be {@code null}.
7071 * @since 2.4
7172 */
72- <T > SqlIdentifierParameterSource forInsert (T instance , Class <T > domainType , Identifier identifier ,
73- IdValueSource idValueSource ) {
73+ <T > SqlIdentifierParameterSource forInsert (T instance , Class <T > domainType , Identifier identifier , IdValueSource idValueSource ) {
7474
7575 RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
76- SqlIdentifierParameterSource parameterSource = getParameterSource (instance , persistentEntity , "" ,
77- PersistentProperty ::isIdProperty );
76+ SqlIdentifierParameterSource parameterSource = getParameterSource (instance , persistentEntity , "" , PersistentProperty ::isIdProperty );
7877
7978 identifier .forEach ((name , value , type ) -> addConvertedPropertyValue (parameterSource , name , value , type ));
8079
@@ -96,21 +95,20 @@ <T> SqlIdentifierParameterSource forInsert(T instance, Class<T> domainType, Iden
9695 /**
9796 * Creates the parameters for a SQL update operation.
9897 *
99- * @param instance the entity to be updated. Must not be {@code null}.
98+ * @param instance the entity to be updated. Must not be {@code null}.
10099 * @param domainType the type of the instance. Must not be {@code null}.
101100 * @return the {@link SqlIdentifierParameterSource} for the update. Guaranteed to not be {@code null}.
102101 * @since 2.4
103102 */
104103 <T > SqlIdentifierParameterSource forUpdate (T instance , Class <T > domainType ) {
105104
106- return getParameterSource (instance , getRequiredPersistentEntity (domainType ), "" ,
107- RelationalPersistentProperty ::isInsertOnly );
105+ return getParameterSource (instance , getRequiredPersistentEntity (domainType ), "" , RelationalPersistentProperty ::isInsertOnly );
108106 }
109107
110108 /**
111109 * Creates the parameters for a SQL query by id.
112110 *
113- * @param id the entity id. Must not be {@code null}.
111+ * @param id the entity id. Must not be {@code null}.
114112 * @param domainType the type of the instance. Must not be {@code null}.
115113 * @return the {@link SqlIdentifierParameterSource} for the query. Guaranteed to not be {@code null}.
116114 * @since 2.4
@@ -122,37 +120,26 @@ <T> SqlIdentifierParameterSource forQueryById(Object id, Class<T> domainType) {
122120 RelationalPersistentEntity <T > entity = getRequiredPersistentEntity (domainType );
123121 RelationalPersistentProperty singleIdProperty = entity .getRequiredIdProperty ();
124122
125- if (singleIdProperty .isEntity ()) {
126-
127- RelationalPersistentEntity <?> complexId = context .getPersistentEntity (singleIdProperty );
128- PersistentPropertyPathAccessor <Object > accessor = complexId .getPropertyPathAccessor (id );
123+ RelationalPersistentEntity <?> complexId = context .getPersistentEntity (singleIdProperty );
129124
130- context .getAggregatePath (entity ).getTableInfo ().idColumnInfos ().forEach ((ap , ci ) -> {
131- Object idValue = accessor .getProperty (ap .getRequiredPersistentPropertyPath ());
125+ Function <AggregatePath , Object > valueExtractor = complexId == null
126+ ? ap -> id
127+ : ap -> complexId .getPropertyPathAccessor (id ).getProperty (ap .getRequiredPersistentPropertyPath ());
132128
133- addConvertedPropertyValue ( //
129+ context .getAggregatePath (entity ).getTableInfo ().idColumnInfos () //
130+ .forEach ((ap , ci ) -> addConvertedPropertyValue ( //
134131 parameterSource , //
135132 ap .getRequiredLeafProperty (), //
136- idValue , //
133+ valueExtractor . apply ( ap ) , //
137134 ci .name () //
138- );
139- });
140- } else {
141-
142- addConvertedPropertyValue ( //
143- parameterSource , //
144- singleIdProperty , //
145- id , //
146- singleIdProperty .getColumnName () //
147- );
148- }
135+ ));
149136 return parameterSource ;
150137 }
151138
152139 /**
153140 * Creates the parameters for a SQL query by ids.
154141 *
155- * @param ids the entity ids. Must not be {@code null}.
142+ * @param ids the entity ids. Must not be {@code null}.
156143 * @param domainType the type of the instance. Must not be {@code null}.
157144 * @return the {@link SqlIdentifierParameterSource} for the query. Guaranteed to not be {@code null}.
158145 * @since 2.4
@@ -184,8 +171,7 @@ <T> SqlIdentifierParameterSource forQueryByIds(Iterable<?> ids, Class<T> domainT
184171
185172 parameterSource .addValue (SqlGenerator .IDS_SQL_PARAMETER , parameterValues );
186173 } else {
187- addConvertedPropertyValuesAsList (parameterSource , getRequiredPersistentEntity (domainType ).getRequiredIdProperty (),
188- ids );
174+ addConvertedPropertyValuesAsList (parameterSource , getRequiredPersistentEntity (domainType ).getRequiredIdProperty (), ids );
189175 }
190176 return parameterSource ;
191177 }
@@ -201,27 +187,22 @@ SqlIdentifierParameterSource forQueryByIdentifier(Identifier identifier) {
201187
202188 SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource ();
203189
204- identifier .toMap ()
205- .forEach ((name , value ) -> addConvertedPropertyValue (parameterSource , name , value , value .getClass ()));
190+ identifier .toMap ().forEach ((name , value ) -> addConvertedPropertyValue (parameterSource , name , value , value .getClass ()));
206191
207192 return parameterSource ;
208193 }
209194
210- private void addConvertedPropertyValue (SqlIdentifierParameterSource parameterSource ,
211- RelationalPersistentProperty property , @ Nullable Object value , SqlIdentifier name ) {
195+ private void addConvertedPropertyValue (SqlIdentifierParameterSource parameterSource , RelationalPersistentProperty property , @ Nullable Object value , SqlIdentifier name ) {
212196
213- addConvertedValue (parameterSource , value , name , converter .getColumnType (property ),
214- converter .getTargetSqlType (property ));
197+ addConvertedValue (parameterSource , value , name , converter .getColumnType (property ), converter .getTargetSqlType (property ));
215198 }
216199
217- private void addConvertedPropertyValue (SqlIdentifierParameterSource parameterSource , SqlIdentifier name , Object value ,
218- Class <?> javaType ) {
200+ private void addConvertedPropertyValue (SqlIdentifierParameterSource parameterSource , SqlIdentifier name , Object value , Class <?> javaType ) {
219201
220202 addConvertedValue (parameterSource , value , name , javaType , JdbcUtil .targetSqlTypeFor (javaType ));
221203 }
222204
223- private void addConvertedValue (SqlIdentifierParameterSource parameterSource , @ Nullable Object value ,
224- SqlIdentifier paramName , Class <?> javaType , SQLType sqlType ) {
205+ private void addConvertedValue (SqlIdentifierParameterSource parameterSource , @ Nullable Object value , SqlIdentifier paramName , Class <?> javaType , SQLType sqlType ) {
225206
226207 JdbcValue jdbcValue = converter .writeJdbcValue ( //
227208 value , //
@@ -235,8 +216,7 @@ private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nu
235216 jdbcValue .getJdbcType ().getVendorTypeNumber ());
236217 }
237218
238- private void addConvertedPropertyValuesAsList (SqlIdentifierParameterSource parameterSource ,
239- RelationalPersistentProperty property , Iterable <?> values ) {
219+ private void addConvertedPropertyValuesAsList (SqlIdentifierParameterSource parameterSource , RelationalPersistentProperty property , Iterable <?> values ) {
240220
241221 List <Object > convertedIds = new ArrayList <>();
242222 JdbcValue jdbcValue = null ;
@@ -262,14 +242,11 @@ private <S> RelationalPersistentEntity<S> getRequiredPersistentEntity(Class<S> d
262242 return (RelationalPersistentEntity <S >) context .getRequiredPersistentEntity (domainType );
263243 }
264244
265- private <S , T > SqlIdentifierParameterSource getParameterSource (@ Nullable S instance ,
266- RelationalPersistentEntity <S > persistentEntity , String prefix ,
267- Predicate <RelationalPersistentProperty > skipProperty ) {
245+ private <S , T > SqlIdentifierParameterSource getParameterSource (@ Nullable S instance , RelationalPersistentEntity <S > persistentEntity , String prefix , Predicate <RelationalPersistentProperty > skipProperty ) {
268246
269247 SqlIdentifierParameterSource parameters = new SqlIdentifierParameterSource ();
270248
271- PersistentPropertyAccessor <S > propertyAccessor = instance != null ? persistentEntity .getPropertyAccessor (instance )
272- : NoValuePropertyAccessor .instance ();
249+ PersistentPropertyAccessor <S > propertyAccessor = instance != null ? persistentEntity .getPropertyAccessor (instance ) : NoValuePropertyAccessor .instance ();
273250
274251 persistentEntity .doWithAll (property -> {
275252
@@ -284,8 +261,7 @@ private <S, T> SqlIdentifierParameterSource getParameterSource(@Nullable S insta
284261
285262 Object value = propertyAccessor .getProperty (property );
286263 RelationalPersistentEntity <?> embeddedEntity = context .getPersistentEntity (property .getTypeInformation ());
287- SqlIdentifierParameterSource additionalParameters = getParameterSource ((T ) value ,
288- (RelationalPersistentEntity <T >) embeddedEntity , prefix + property .getEmbeddedPrefix (), skipProperty );
264+ SqlIdentifierParameterSource additionalParameters = getParameterSource ((T ) value , (RelationalPersistentEntity <T >) embeddedEntity , prefix + property .getEmbeddedPrefix (), skipProperty );
289265 parameters .addAll (additionalParameters );
290266 } else {
291267
0 commit comments