27
27
import org .springframework .dao .EmptyResultDataAccessException ;
28
28
import org .springframework .dao .InvalidDataAccessApiUsageException ;
29
29
import org .springframework .dao .NonTransientDataAccessException ;
30
- import org .springframework .data .jdbc .core .mapping .BasicJdbcPersistentEntityInformation ;
31
30
import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
32
31
import org .springframework .data .jdbc .core .mapping .JdbcPersistentEntity ;
33
- import org .springframework .data .jdbc .core .mapping .JdbcPersistentEntityInformation ;
34
32
import org .springframework .data .jdbc .core .mapping .JdbcPersistentProperty ;
35
33
import org .springframework .data .jdbc .support .JdbcUtil ;
34
+ import org .springframework .data .mapping .PersistentPropertyAccessor ;
36
35
import org .springframework .data .mapping .PropertyHandler ;
37
36
import org .springframework .data .mapping .PropertyPath ;
38
- import org .springframework .data .repository . core . EntityInformation ;
37
+ import org .springframework .data .mapping . model . ConvertingPropertyAccessor ;
39
38
import org .springframework .jdbc .core .RowMapper ;
40
39
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
41
40
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
@@ -58,8 +57,8 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
58
57
59
58
private final @ NonNull SqlGeneratorSource sqlGeneratorSource ;
60
59
private final @ NonNull JdbcMappingContext context ;
61
- private final @ NonNull DataAccessStrategy accessStrategy ;
62
60
private final @ NonNull NamedParameterJdbcOperations operations ;
61
+ private final @ NonNull DataAccessStrategy accessStrategy ;
63
62
64
63
/**
65
64
* Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
@@ -79,8 +78,6 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
79
78
80
79
KeyHolder holder = new GeneratedKeyHolder ();
81
80
JdbcPersistentEntity <T > persistentEntity = getRequiredPersistentEntity (domainType );
82
- JdbcPersistentEntityInformation <T , ?> entityInformation = context
83
- .getRequiredPersistentEntityInformation (domainType );
84
81
85
82
MapSqlParameterSource parameterSource = getPropertyMap (instance , persistentEntity );
86
83
@@ -103,7 +100,8 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
103
100
104
101
// if there is an id property and it was null before the save
105
102
// The database should have created an id and provided it.
106
- if (idProperty != null && idValue == null && entityInformation .isNew (instance )) {
103
+
104
+ if (idProperty != null && idValue == null && persistentEntity .isNew (instance )) {
107
105
throw new IllegalStateException (String .format (ENTITY_NEW_AFTER_INSERT , persistentEntity ));
108
106
}
109
107
}
@@ -278,15 +276,12 @@ private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersisten
278
276
@ SuppressWarnings ("unchecked" )
279
277
private <S , ID > ID getIdValueOrNull (S instance , JdbcPersistentEntity <S > persistentEntity ) {
280
278
281
- EntityInformation <S , ID > entityInformation = (EntityInformation <S , ID >) context
282
- .getRequiredPersistentEntityInformation (persistentEntity .getType ());
283
-
284
- ID idValue = entityInformation .getId (instance );
279
+ ID idValue = (ID ) persistentEntity .getIdentifierAccessor (instance ).getIdentifier ();
285
280
286
281
return isIdPropertyNullOrScalarZero (idValue , persistentEntity ) ? null : idValue ;
287
282
}
288
283
289
- private <S , ID > boolean isIdPropertyNullOrScalarZero (ID idValue , JdbcPersistentEntity <S > persistentEntity ) {
284
+ private static <S , ID > boolean isIdPropertyNullOrScalarZero (ID idValue , JdbcPersistentEntity <S > persistentEntity ) {
290
285
291
286
JdbcPersistentProperty idProperty = persistentEntity .getIdProperty ();
292
287
return idValue == null //
@@ -297,16 +292,16 @@ private <S, ID> boolean isIdPropertyNullOrScalarZero(ID idValue, JdbcPersistentE
297
292
298
293
private <S > void setIdFromJdbc (S instance , KeyHolder holder , JdbcPersistentEntity <S > persistentEntity ) {
299
294
300
- JdbcPersistentEntityInformation <S , ?> entityInformation = new BasicJdbcPersistentEntityInformation <>(
301
- persistentEntity );
302
-
303
295
try {
304
296
305
297
getIdFromHolder (holder , persistentEntity ).ifPresent (it -> {
306
298
307
- Class <?> targetType = persistentEntity .getRequiredIdProperty ().getType ();
308
- Object converted = convert (it , targetType );
309
- entityInformation .setId (instance , converted );
299
+ PersistentPropertyAccessor accessor = persistentEntity .getPropertyAccessor (instance );
300
+ ConvertingPropertyAccessor convertingPropertyAccessor = new ConvertingPropertyAccessor (accessor ,
301
+ context .getConversions ());
302
+ JdbcPersistentProperty idProperty = persistentEntity .getRequiredIdProperty ();
303
+
304
+ convertingPropertyAccessor .setProperty (idProperty , it );
310
305
});
311
306
312
307
} catch (NonTransientDataAccessException e ) {
0 commit comments