@@ -124,7 +124,7 @@ <T> Entity<T> forEntity(T entity) {
124
124
return new SimpleMappedEntity ((Map <String , Object >) entity );
125
125
}
126
126
127
- return MappedEntity .of (entity , context );
127
+ return MappedEntity .of (entity , context , this );
128
128
}
129
129
130
130
/**
@@ -148,7 +148,7 @@ <T> AdaptibleEntity<T> forEntity(T entity, ConversionService conversionService)
148
148
return new SimpleMappedEntity ((Map <String , Object >) entity );
149
149
}
150
150
151
- return AdaptibleMappedEntity .of (entity , context , conversionService );
151
+ return AdaptibleMappedEntity .of (entity , context , conversionService , this );
152
152
}
153
153
154
154
/**
@@ -382,6 +382,16 @@ interface Entity<T> {
382
382
*/
383
383
Object getId ();
384
384
385
+ /**
386
+ * Returns the property value for {@code key}.
387
+ *
388
+ * @param key
389
+ * @return
390
+ * @since 4.1
391
+ */
392
+ @ Nullable
393
+ Object getPropertyValue (String key );
394
+
385
395
/**
386
396
* Returns the {@link Query} to find the entity by its identifier.
387
397
*
@@ -453,6 +463,11 @@ default boolean isVersionedEntity() {
453
463
*/
454
464
boolean isNew ();
455
465
466
+ /**
467
+ * @param sortObject
468
+ * @return
469
+ * @since 3.1
470
+ */
456
471
Map <String , Object > extractKeys (Document sortObject );
457
472
458
473
}
@@ -514,7 +529,12 @@ public String getIdFieldName() {
514
529
515
530
@ Override
516
531
public Object getId () {
517
- return map .get (ID_FIELD );
532
+ return getPropertyValue (ID_FIELD );
533
+ }
534
+
535
+ @ Override
536
+ public Object getPropertyValue (String key ) {
537
+ return map .get (key );
518
538
}
519
539
520
540
@ Override
@@ -609,23 +629,26 @@ private static class MappedEntity<T> implements Entity<T> {
609
629
private final MongoPersistentEntity <?> entity ;
610
630
private final IdentifierAccessor idAccessor ;
611
631
private final PersistentPropertyAccessor <T > propertyAccessor ;
632
+ private final EntityOperations entityOperations ;
612
633
613
634
protected MappedEntity (MongoPersistentEntity <?> entity , IdentifierAccessor idAccessor ,
614
- PersistentPropertyAccessor <T > propertyAccessor ) {
635
+ PersistentPropertyAccessor <T > propertyAccessor , EntityOperations entityOperations ) {
615
636
616
637
this .entity = entity ;
617
638
this .idAccessor = idAccessor ;
618
639
this .propertyAccessor = propertyAccessor ;
640
+ this .entityOperations = entityOperations ;
619
641
}
620
642
621
643
private static <T > MappedEntity <T > of (T bean ,
622
- MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > context ) {
644
+ MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > context ,
645
+ EntityOperations entityOperations ) {
623
646
624
647
MongoPersistentEntity <?> entity = context .getRequiredPersistentEntity (bean .getClass ());
625
648
IdentifierAccessor identifierAccessor = entity .getIdentifierAccessor (bean );
626
649
PersistentPropertyAccessor <T > propertyAccessor = entity .getPropertyAccessor (bean );
627
650
628
- return new MappedEntity <>(entity , identifierAccessor , propertyAccessor );
651
+ return new MappedEntity <>(entity , identifierAccessor , propertyAccessor , entityOperations );
629
652
}
630
653
631
654
@ Override
@@ -638,6 +661,11 @@ public Object getId() {
638
661
return idAccessor .getRequiredIdentifier ();
639
662
}
640
663
664
+ @ Override
665
+ public Object getPropertyValue (String key ) {
666
+ return propertyAccessor .getProperty (entity .getRequiredPersistentProperty (key ));
667
+ }
668
+
641
669
@ Override
642
670
public Query getByIdQuery () {
643
671
@@ -724,13 +752,38 @@ public Map<String, Object> extractKeys(Document sortObject) {
724
752
725
753
for (String key : sortObject .keySet ()) {
726
754
727
- // TODO: make this work for nested properties
728
- MongoPersistentProperty persistentProperty = entity .getRequiredPersistentProperty (key );
729
- keyset .put (key , propertyAccessor .getProperty (persistentProperty ));
755
+ if (key .indexOf ('.' ) != -1 ) {
756
+
757
+ // follow the path across nested levels.
758
+ // TODO: We should have a MongoDB-specific property path abstraction to allow diving into Document.
759
+ keyset .put (key , getNestedPropertyValue (key ));
760
+ } else {
761
+ keyset .put (key , getPropertyValue (key ));
762
+ }
730
763
}
731
764
732
765
return keyset ;
733
766
}
767
+
768
+ @ Nullable
769
+ private Object getNestedPropertyValue (String key ) {
770
+
771
+ String [] segments = key .split ("\\ ." );
772
+ Entity <?> currentEntity = this ;
773
+ Object currentValue = null ;
774
+
775
+ for (int i = 0 ; i < segments .length ; i ++) {
776
+
777
+ String segment = segments [i ];
778
+ currentValue = currentEntity .getPropertyValue (segment );
779
+
780
+ if (i < segments .length - 1 ) {
781
+ currentEntity = entityOperations .forEntity (currentValue );
782
+ }
783
+ }
784
+
785
+ return currentValue ;
786
+ }
734
787
}
735
788
736
789
private static class AdaptibleMappedEntity <T > extends MappedEntity <T > implements AdaptibleEntity <T > {
@@ -740,9 +793,9 @@ private static class AdaptibleMappedEntity<T> extends MappedEntity<T> implements
740
793
private final IdentifierAccessor identifierAccessor ;
741
794
742
795
private AdaptibleMappedEntity (MongoPersistentEntity <?> entity , IdentifierAccessor identifierAccessor ,
743
- ConvertingPropertyAccessor <T > propertyAccessor ) {
796
+ ConvertingPropertyAccessor <T > propertyAccessor , EntityOperations entityOperations ) {
744
797
745
- super (entity , identifierAccessor , propertyAccessor );
798
+ super (entity , identifierAccessor , propertyAccessor , entityOperations );
746
799
747
800
this .entity = entity ;
748
801
this .propertyAccessor = propertyAccessor ;
@@ -751,14 +804,14 @@ private AdaptibleMappedEntity(MongoPersistentEntity<?> entity, IdentifierAccesso
751
804
752
805
private static <T > AdaptibleEntity <T > of (T bean ,
753
806
MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > context ,
754
- ConversionService conversionService ) {
807
+ ConversionService conversionService , EntityOperations entityOperations ) {
755
808
756
809
MongoPersistentEntity <?> entity = context .getRequiredPersistentEntity (bean .getClass ());
757
810
IdentifierAccessor identifierAccessor = entity .getIdentifierAccessor (bean );
758
811
PersistentPropertyAccessor <T > propertyAccessor = entity .getPropertyAccessor (bean );
759
812
760
813
return new AdaptibleMappedEntity <>(entity , identifierAccessor ,
761
- new ConvertingPropertyAccessor <>(propertyAccessor , conversionService ));
814
+ new ConvertingPropertyAccessor <>(propertyAccessor , conversionService ), entityOperations );
762
815
}
763
816
764
817
@ Nullable
0 commit comments