72
72
* <br />
73
73
* <strong>NOTE</strong> {@link MappingRedisConverter} is an {@link InitializingBean} and requires
74
74
* {@link MappingRedisConverter#afterPropertiesSet()} to be called.
75
- *
75
+ *
76
76
* <pre>
77
77
* <code>
78
78
* @RedisHash("persons")
79
79
* class Person {
80
- *
80
+ *
81
81
* @Id String id;
82
82
* String firstname;
83
- *
83
+ *
84
84
* List<String> nicknames;
85
85
* List<Person> coworkers;
86
- *
86
+ *
87
87
* Address address;
88
88
* @Reference Country nationality;
89
89
* }
105
105
* nationality=nationality:andora
106
106
* </code>
107
107
* </pre>
108
- *
108
+ *
109
109
* @author Christoph Strobl
110
110
* @author Greg Turnquist
111
+ * @author Mark Paluch
111
112
* @since 1.7
112
113
*/
113
114
public class MappingRedisConverter implements RedisConverter , InitializingBean {
@@ -128,7 +129,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
128
129
129
130
/**
130
131
* Creates new {@link MappingRedisConverter}.
131
- *
132
+ *
132
133
* @param context can be {@literal null}.
133
134
*/
134
135
MappingRedisConverter (RedisMappingContext context ) {
@@ -137,7 +138,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
137
138
138
139
/**
139
140
* Creates new {@link MappingRedisConverter} and defaults {@link RedisMappingContext} when {@literal null}.
140
- *
141
+ *
141
142
* @param mappingContext can be {@literal null}.
142
143
* @param indexResolver can be {@literal null}.
143
144
* @param referenceResolver must not be {@literal null}.
@@ -394,8 +395,8 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
394
395
RedisPersistentEntity <?> entity = mappingContext .getPersistentEntity (update .getTarget ());
395
396
396
397
write (update .getValue (), sink );
397
- if (sink .getBucket ().keySet ().contains ("_class" )) {
398
- sink .getBucket ().put ("_class" , null ); // overwrite stuff in here
398
+ if (sink .getBucket ().keySet ().contains (TYPE_HINT_ALIAS )) {
399
+ sink .getBucket ().put (TYPE_HINT_ALIAS , null ); // overwrite stuff in here
399
400
}
400
401
401
402
if (update .isRefreshTtl () && !update .getPropertyUpdates ().isEmpty ()) {
@@ -411,86 +412,95 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
411
412
String path = pUpdate .getPropertyPath ();
412
413
413
414
if (UpdateCommand .SET .equals (pUpdate .getCmd ())) {
415
+ writePartialPropertyUpdate (update , pUpdate , sink , entity , path );
416
+ }
417
+ }
418
+ }
414
419
415
- KeyValuePersistentProperty targetProperty = getTargetPropertyOrNullForPath (path , update .getTarget ());
420
+ /**
421
+ * @param update
422
+ * @param pUpdate
423
+ * @param sink
424
+ * @param entity
425
+ * @param path
426
+ */
427
+ private void writePartialPropertyUpdate (PartialUpdate <?> update , PropertyUpdate pUpdate , RedisData sink ,
428
+ RedisPersistentEntity <?> entity , String path ) {
416
429
417
- if ( targetProperty == null ) {
430
+ KeyValuePersistentProperty targetProperty = getTargetPropertyOrNullForPath ( path , update . getTarget ());
418
431
419
- targetProperty = getTargetPropertyOrNullForPath ( path . replaceAll ( " \\ . \\ [.* \\ ]" , "" ), update . getTarget ());
432
+ if ( targetProperty == null ) {
420
433
421
- TypeInformation <?> ti = targetProperty == null ? ClassTypeInformation .OBJECT
422
- : (targetProperty .isMap ()
423
- ? (targetProperty .getTypeInformation ().getMapValueType () != null
424
- ? targetProperty .getTypeInformation ().getMapValueType () : ClassTypeInformation .OBJECT )
425
- : targetProperty .getTypeInformation ().getActualType ());
434
+ targetProperty = getTargetPropertyOrNullForPath (path .replaceAll ("\\ .\\ [.*\\ ]" , "" ), update .getTarget ());
426
435
427
- writeInternal (entity .getKeySpace (), pUpdate .getPropertyPath (), pUpdate .getValue (), ti , sink );
428
- continue ;
429
- }
436
+ TypeInformation <?> ti = targetProperty == null ? ClassTypeInformation .OBJECT
437
+ : (targetProperty .isMap ()
438
+ ? (targetProperty .getTypeInformation ().getMapValueType () != null
439
+ ? targetProperty .getTypeInformation ().getMapValueType () : ClassTypeInformation .OBJECT )
440
+ : targetProperty .getTypeInformation ().getActualType ());
430
441
431
- if (targetProperty .isAssociation ()) {
442
+ writeInternal (entity .getKeySpace (), pUpdate .getPropertyPath (), pUpdate .getValue (), ti , sink );
443
+ return ;
444
+ }
432
445
433
- if (targetProperty .isCollectionLike ()) {
446
+ if (targetProperty .isAssociation ()) {
434
447
435
- KeyValuePersistentEntity <?> ref = mappingContext .getPersistentEntity (
436
- targetProperty .getAssociation ().getInverse ().getTypeInformation ().getComponentType ().getActualType ());
448
+ if (targetProperty .isCollectionLike ()) {
437
449
438
- int i = 0 ;
439
- for ( Object o : ( Collection <?>) pUpdate . getValue ()) {
450
+ KeyValuePersistentEntity <?> ref = mappingContext . getPersistentEntity (
451
+ targetProperty . getAssociation (). getInverse (). getTypeInformation (). getComponentType (). getActualType ());
440
452
441
- Object refId = ref .getPropertyAccessor (o ).getProperty (ref .getIdProperty ());
442
- sink .getBucket ().put (pUpdate .getPropertyPath () + ".[" + i + "]" ,
443
- toBytes (ref .getKeySpace () + ":" + refId ));
444
- i ++;
445
- }
446
- } else {
447
-
448
- KeyValuePersistentEntity <?> ref = mappingContext
449
- .getPersistentEntity (targetProperty .getAssociation ().getInverse ().getTypeInformation ());
453
+ int i = 0 ;
454
+ for (Object o : (Collection <?>) pUpdate .getValue ()) {
450
455
451
- Object refId = ref .getPropertyAccessor (pUpdate . getValue () ).getProperty (ref .getIdProperty ());
452
- sink .getBucket ().put (pUpdate .getPropertyPath (), toBytes (ref .getKeySpace () + ":" + refId ));
453
- }
456
+ Object refId = ref .getPropertyAccessor (o ).getProperty (ref .getIdProperty ());
457
+ sink .getBucket ().put (pUpdate .getPropertyPath () + ".[" + i + "]" , toBytes (ref .getKeySpace () + ":" + refId ));
458
+ i ++;
454
459
}
460
+ } else {
455
461
456
- else if (targetProperty .isCollectionLike ()) {
462
+ KeyValuePersistentEntity <?> ref = mappingContext
463
+ .getPersistentEntity (targetProperty .getAssociation ().getInverse ().getTypeInformation ());
457
464
458
- Collection <?> collection = pUpdate .getValue () instanceof Collection ? (Collection <?>) pUpdate .getValue ()
459
- : Collections .<Object > singleton (pUpdate .getValue ());
460
- writeCollection (entity .getKeySpace (), pUpdate .getPropertyPath (), collection ,
461
- targetProperty .getTypeInformation ().getActualType (), sink );
462
- } else if (targetProperty .isMap ()) {
465
+ Object refId = ref .getPropertyAccessor (pUpdate .getValue ()).getProperty (ref .getIdProperty ());
466
+ sink .getBucket ().put (pUpdate .getPropertyPath (), toBytes (ref .getKeySpace () + ":" + refId ));
467
+ }
468
+ } else if (targetProperty .isCollectionLike ()) {
463
469
464
- Map <Object , Object > map = new HashMap <Object , Object >();
470
+ Collection <?> collection = pUpdate .getValue () instanceof Collection ? (Collection <?>) pUpdate .getValue ()
471
+ : Collections .<Object > singleton (pUpdate .getValue ());
472
+ writeCollection (entity .getKeySpace (), pUpdate .getPropertyPath (), collection ,
473
+ targetProperty .getTypeInformation ().getActualType (), sink );
474
+ } else if (targetProperty .isMap ()) {
465
475
466
- if (pUpdate .getValue () instanceof Map ) {
467
- map .putAll ((Map <?, ?>) pUpdate .getValue ());
468
- } else if (pUpdate .getValue () instanceof Map .Entry ) {
469
- map .put (((Map .Entry <?, ?>) pUpdate .getValue ()).getKey (), ((Map .Entry <?, ?>) pUpdate .getValue ()).getValue ());
470
- } else {
471
- throw new MappingException (
472
- String .format ("Cannot set update value for map property '%s' to '%s'. Please use a Map or Map.Entry." ,
473
- pUpdate .getPropertyPath (), pUpdate .getValue ()));
474
- }
476
+ Map <Object , Object > map = new HashMap <Object , Object >();
475
477
476
- writeMap (entity .getKeySpace (), pUpdate .getPropertyPath (), targetProperty .getMapValueType (), map , sink );
477
- } else {
478
+ if (pUpdate .getValue () instanceof Map ) {
479
+ map .putAll ((Map <?, ?>) pUpdate .getValue ());
480
+ } else if (pUpdate .getValue () instanceof Entry ) {
481
+ map .put (((Entry <?, ?>) pUpdate .getValue ()).getKey (), ((Entry <?, ?>) pUpdate .getValue ()).getValue ());
482
+ } else {
483
+ throw new MappingException (
484
+ String .format ("Cannot set update value for map property '%s' to '%s'. Please use a Map or Map.Entry." ,
485
+ pUpdate .getPropertyPath (), pUpdate .getValue ()));
486
+ }
478
487
479
- writeInternal (entity .getKeySpace (), pUpdate .getPropertyPath (), pUpdate . getValue (),
480
- targetProperty . getTypeInformation (), sink );
488
+ writeMap (entity .getKeySpace (), pUpdate .getPropertyPath (), targetProperty . getMapValueType (), map , sink );
489
+ } else {
481
490
482
- Set < IndexedData > data = indexResolver . resolveIndexesFor (entity .getKeySpace (), pUpdate .getPropertyPath (),
483
- targetProperty .getTypeInformation (), pUpdate . getValue () );
491
+ writeInternal (entity .getKeySpace (), pUpdate .getPropertyPath (), pUpdate . getValue (),
492
+ targetProperty .getTypeInformation (), sink );
484
493
485
- if (data .isEmpty ()) {
494
+ Set <IndexedData > data = indexResolver .resolveIndexesFor (entity .getKeySpace (), pUpdate .getPropertyPath (),
495
+ targetProperty .getTypeInformation (), pUpdate .getValue ());
486
496
487
- data = indexResolver .resolveIndexesFor (entity .getKeySpace (), pUpdate .getPropertyPath (),
488
- targetProperty .getOwner ().getTypeInformation (), pUpdate .getValue ());
497
+ if (data .isEmpty ()) {
498
+
499
+ data = indexResolver .resolveIndexesFor (entity .getKeySpace (), pUpdate .getPropertyPath (),
500
+ targetProperty .getOwner ().getTypeInformation (), pUpdate .getValue ());
489
501
490
- }
491
- sink .addIndexedData (data );
492
- }
493
502
}
503
+ sink .addIndexedData (data );
494
504
}
495
505
}
496
506
@@ -589,10 +599,10 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
589
599
}
590
600
});
591
601
592
- writeAssiciation ( keyspace , path , entity , value , sink );
602
+ writeAssociation ( path , entity , value , sink );
593
603
}
594
604
595
- private void writeAssiciation ( final String keyspace , final String path , final KeyValuePersistentEntity <?> entity ,
605
+ private void writeAssociation ( final String path , final KeyValuePersistentEntity <?> entity ,
596
606
final Object value , final RedisData sink ) {
597
607
598
608
if (value == null ) {
@@ -878,7 +888,7 @@ private Class<?> getTypeHint(String path, Bucket bucket, Class<?> fallback) {
878
888
879
889
/**
880
890
* Convert given source to binary representation using the underlying {@link ConversionService}.
881
- *
891
+ *
882
892
* @param source
883
893
* @return
884
894
* @throws ConverterNotFoundException
@@ -894,7 +904,7 @@ public byte[] toBytes(Object source) {
894
904
895
905
/**
896
906
* Convert given binary representation to desired target type using the underlying {@link ConversionService}.
897
- *
907
+ *
898
908
* @param source
899
909
* @param type
900
910
* @return
@@ -934,7 +944,7 @@ private Object toArray(Collection<Object> source, Class<?> arrayType, Class<?> v
934
944
935
945
/**
936
946
* Set {@link CustomConversions} to be applied.
937
- *
947
+ *
938
948
* @param customConversions
939
949
*/
940
950
public void setCustomConversions (CustomConversions customConversions ) {
0 commit comments