29
29
import org .springframework .dao .DataRetrievalFailureException ;
30
30
import org .springframework .dao .EmptyResultDataAccessException ;
31
31
import org .springframework .dao .InvalidDataAccessApiUsageException ;
32
+ import org .springframework .data .jdbc .core .convert .BasicJdbcConverter ;
32
33
import org .springframework .data .jdbc .support .JdbcUtil ;
33
34
import org .springframework .data .mapping .PersistentPropertyAccessor ;
34
35
import org .springframework .data .mapping .PersistentPropertyPath ;
37
38
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
38
39
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
39
40
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
41
+ import org .springframework .data .relational .domain .Identifier ;
40
42
import org .springframework .data .util .ClassTypeInformation ;
41
43
import org .springframework .jdbc .core .RowMapper ;
42
44
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
@@ -87,23 +89,24 @@ public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, Relation
87
89
*/
88
90
@ Override
89
91
public <T > Object insert (T instance , Class <T > domainType , Map <String , Object > additionalParameters ) {
90
- return insert (instance , domainType , ParentKeys .fromNamedValues (additionalParameters ));
92
+ return insert (instance , domainType , BasicJdbcConverter .fromNamedValues (additionalParameters ));
91
93
}
92
94
93
95
/*
94
96
* (non-Javadoc)
95
97
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
96
98
*/
97
99
@ Override
98
- public <T > Object insert (T instance , Class <T > domainType , ParentKeys parentKeys ) {
100
+ public <T > Object insert (T instance , Class <T > domainType , Identifier identifier ) {
99
101
100
102
KeyHolder holder = new GeneratedKeyHolder ();
101
103
RelationalPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
102
104
103
105
Map <String , Object > parameters = new LinkedHashMap <>();
104
- for (ParentKeys .ParentKey parameter : parentKeys .getParameters ()) {
105
- parameters .put (parameter .getName (), converter .writeValue (parameter .getValue (), ClassTypeInformation .from (parameter .getTargetType ())));
106
- }
106
+ identifier .forEach (identifierValue -> {
107
+ parameters .put (identifierValue .getName (),
108
+ converter .writeValue (identifierValue .getValue (), ClassTypeInformation .from (identifierValue .getTargetType ())));
109
+ });
107
110
108
111
MapSqlParameterSource parameterSource = getPropertyMap (instance , persistentEntity , "" );
109
112
@@ -295,7 +298,8 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
295
298
return result ;
296
299
}
297
300
298
- private <S , T > MapSqlParameterSource getPropertyMap (final S instance , RelationalPersistentEntity <S > persistentEntity , String prefix ) {
301
+ private <S , T > MapSqlParameterSource getPropertyMap (final S instance , RelationalPersistentEntity <S > persistentEntity ,
302
+ String prefix ) {
299
303
300
304
MapSqlParameterSource parameters = new MapSqlParameterSource ();
301
305
@@ -307,23 +311,26 @@ private <S, T> MapSqlParameterSource getPropertyMap(final S instance, Relational
307
311
return ;
308
312
}
309
313
310
- if (property .isEmbedded ()){
314
+ if (property .isEmbedded ()) {
311
315
312
316
Object value = propertyAccessor .getProperty (property );
313
- final RelationalPersistentEntity <?> embeddedEntity = context .getPersistentEntity (property .getType ());
314
- final MapSqlParameterSource additionalParameters = getPropertyMap ((T )value , (RelationalPersistentEntity <T >) embeddedEntity , prefix + property .getEmbeddedPrefix ());
317
+ final RelationalPersistentEntity <?> embeddedEntity = context .getPersistentEntity (property .getType ());
318
+ final MapSqlParameterSource additionalParameters = getPropertyMap ((T ) value ,
319
+ (RelationalPersistentEntity <T >) embeddedEntity , prefix + property .getEmbeddedPrefix ());
315
320
parameters .addValues (additionalParameters .getValues ());
316
321
} else {
317
322
318
323
Object value = propertyAccessor .getProperty (property );
319
324
Object convertedValue = convertForWrite (property , value );
320
325
321
- parameters .addValue (prefix + property .getColumnName (), convertedValue , JdbcUtil .sqlTypeFor (property .getColumnType ()));
326
+ parameters .addValue (prefix + property .getColumnName (), convertedValue ,
327
+ JdbcUtil .sqlTypeFor (property .getColumnType ()));
322
328
}
323
329
});
324
330
325
331
return parameters ;
326
332
}
333
+
327
334
@ Nullable
328
335
private Object convertForWrite (RelationalPersistentProperty property , @ Nullable Object value ) {
329
336
@@ -340,9 +347,8 @@ private Object convertForWrite(RelationalPersistentProperty property, @Nullable
340
347
341
348
String typeName = JDBCType .valueOf (JdbcUtil .sqlTypeFor (componentType )).getName ();
342
349
343
- return operations .getJdbcOperations ().execute (
344
- (Connection c ) -> c .createArrayOf (typeName , (Object []) convertedValue )
345
- );
350
+ return operations .getJdbcOperations ()
351
+ .execute ((Connection c ) -> c .createArrayOf (typeName , (Object []) convertedValue ));
346
352
}
347
353
348
354
@ SuppressWarnings ("unchecked" )
@@ -367,22 +373,22 @@ private static <S, ID> boolean isIdPropertyNullOrScalarZero(@Nullable ID idValue
367
373
@ Nullable
368
374
private <S > Object getIdFromHolder (KeyHolder holder , RelationalPersistentEntity <S > persistentEntity ) {
369
375
370
- try {
371
- // MySQL just returns one value with a special name
372
- return holder .getKey ();
373
- } catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e ) {
374
- // Postgres returns a value for each column
376
+ try {
377
+ // MySQL just returns one value with a special name
378
+ return holder .getKey ();
379
+ } catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e ) {
380
+ // Postgres returns a value for each column
375
381
// MS SQL Server returns a value that might be null.
376
382
377
- Map <String , Object > keys = holder .getKeys ();
383
+ Map <String , Object > keys = holder .getKeys ();
378
384
379
- if (keys == null || persistentEntity .getIdProperty () == null ) {
380
- return null ;
381
- }
385
+ if (keys == null || persistentEntity .getIdProperty () == null ) {
386
+ return null ;
387
+ }
382
388
383
- return keys .get (persistentEntity .getIdColumn ());
384
- }
385
- }
389
+ return keys .get (persistentEntity .getIdColumn ());
390
+ }
391
+ }
386
392
387
393
private EntityRowMapper <?> getEntityRowMapper (Class <?> domainType ) {
388
394
return new EntityRowMapper <>(getRequiredPersistentEntity (domainType ), context , converter , accessStrategy );
0 commit comments