@@ -510,7 +510,6 @@ private <S> S read(ConversionContext context, MongoPersistentEntity<S> entity, D
510
510
S instance = instantiator .createInstance (entity , provider );
511
511
512
512
if (entity .requiresPropertyPopulation ()) {
513
-
514
513
return populateProperties (context , entity , documentAccessor , evaluator , instance );
515
514
}
516
515
@@ -589,14 +588,18 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
589
588
ConversionContext propertyContext = context .forProperty (prop );
590
589
MongoDbPropertyValueProvider valueProviderToUse = valueProvider .withContext (propertyContext );
591
590
592
- if (prop .isAssociation () && ! entity . isCreatorArgument ( prop ) ) {
591
+ if (prop .isAssociation ()) {
593
592
594
593
if (callback == null ) {
595
594
callback = getDbRefResolverCallback (propertyContext , documentAccessor , evaluator );
596
595
}
597
596
598
- readAssociation (prop .getRequiredAssociation (), accessor , documentAccessor , dbRefProxyHandler , callback ,
599
- propertyContext , evaluator );
597
+ Object value = readAssociation (prop .getRequiredAssociation (), documentAccessor , dbRefProxyHandler , callback ,
598
+ propertyContext );
599
+
600
+ if (value != null ) {
601
+ accessor .setProperty (prop , value );
602
+ }
600
603
continue ;
601
604
}
602
605
@@ -611,17 +614,6 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
611
614
continue ;
612
615
}
613
616
614
- if (prop .isAssociation ()) {
615
-
616
- if (callback == null ) {
617
- callback = getDbRefResolverCallback (propertyContext , documentAccessor , evaluator );
618
- }
619
-
620
- readAssociation (prop .getRequiredAssociation (), accessor , documentAccessor , dbRefProxyHandler , callback ,
621
- propertyContext , evaluator );
622
- continue ;
623
- }
624
-
625
617
accessor .setProperty (prop , valueProviderToUse .getPropertyValue (prop ));
626
618
}
627
619
}
@@ -633,9 +625,10 @@ private DbRefResolverCallback getDbRefResolverCallback(ConversionContext context
633
625
(prop , bson , e , path ) -> MappingMongoConverter .this .getValueInternal (context , prop , bson , e ));
634
626
}
635
627
636
- private void readAssociation (Association <MongoPersistentProperty > association , PersistentPropertyAccessor <?> accessor ,
628
+ @ Nullable
629
+ private Object readAssociation (Association <MongoPersistentProperty > association ,
637
630
DocumentAccessor documentAccessor , DbRefProxyHandler handler , DbRefResolverCallback callback ,
638
- ConversionContext context , SpELExpressionEvaluator evaluator ) {
631
+ ConversionContext context ) {
639
632
640
633
MongoPersistentProperty property = association .getInverse ();
641
634
Object value = documentAccessor .get (property );
@@ -648,30 +641,27 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
648
641
if (conversionService .canConvert (DocumentPointer .class , property .getActualType ())) {
649
642
650
643
if (value == null ) {
651
- return ;
644
+ return null ;
652
645
}
653
646
654
647
DocumentPointer <?> pointer = () -> value ;
655
648
656
649
// collection like special treatment
657
- accessor . setProperty ( property , conversionService .convert (pointer , property .getActualType () ));
650
+ return conversionService .convert (pointer , property .getActualType ());
658
651
} else {
659
652
660
- accessor .setProperty (property ,
661
- dbRefResolver .resolveReference (property ,
653
+ return dbRefResolver .resolveReference (property ,
662
654
new DocumentReferenceSource (documentAccessor .getDocument (), documentAccessor .get (property )),
663
- referenceLookupDelegate , context .forProperty (property )::convert ) );
655
+ referenceLookupDelegate , context .forProperty (property )::convert );
664
656
}
665
- return ;
666
657
}
667
658
668
659
if (value == null ) {
669
- return ;
660
+ return null ;
670
661
}
671
662
672
663
if (value instanceof DBRef dbref ) {
673
- accessor .setProperty (property , dbRefResolver .resolveDbRef (property , dbref , callback , handler ));
674
- return ;
664
+ return dbRefResolver .resolveDbRef (property , dbref , callback , handler );
675
665
}
676
666
677
667
/*
@@ -682,18 +672,18 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
682
672
if (value instanceof Document document ) {
683
673
if (property .isMap ()) {
684
674
if (document .isEmpty () || peek (document .values ()) instanceof DBRef ) {
685
- accessor . setProperty ( property , dbRefResolver .resolveDbRef (property , null , callback , handler ) );
675
+ return dbRefResolver .resolveDbRef (property , null , callback , handler );
686
676
} else {
687
- accessor . setProperty ( property , readMap (context , document , property .getTypeInformation () ));
677
+ return readMap (context , document , property .getTypeInformation ());
688
678
}
689
679
} else {
690
- accessor . setProperty ( property , read (property .getActualType (), document ) );
680
+ return read (property .getActualType (), document );
691
681
}
692
682
} else if (value instanceof Collection <?> collection && !collection .isEmpty ()
693
683
&& peek (collection ) instanceof Document ) {
694
- accessor . setProperty ( property , readCollectionOrArray (context , collection , property .getTypeInformation () ));
684
+ return readCollectionOrArray (context , collection , property .getTypeInformation ());
695
685
} else {
696
- accessor . setProperty ( property , dbRefResolver .resolveDbRef (property , null , callback , handler ) );
686
+ return dbRefResolver .resolveDbRef (property , null , callback , handler );
697
687
}
698
688
}
699
689
@@ -1965,26 +1955,14 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1965
1955
1966
1956
ConversionContext propertyContext = context .forProperty (property );
1967
1957
1968
- if (property .isDbReference () && property .getDBRef ().lazy ()) {
1969
-
1970
- Object rawRefValue = accessor .get (property );
1971
- if (rawRefValue == null ) {
1972
- return null ;
1973
- }
1958
+ if (property .isAssociation ()) {
1974
1959
1975
1960
DbRefResolverCallback callback = new DefaultDbRefResolverCallback (accessor .getDocument (), context .getPath (),
1976
1961
evaluator , (prop , bson , evaluator , path ) -> MappingMongoConverter .this .getValueInternal (context , prop , bson ,
1977
1962
evaluator ));
1978
1963
1979
- DBRef dbref = rawRefValue instanceof DBRef ? (DBRef ) rawRefValue : null ;
1980
- return (T ) dbRefResolver .resolveDbRef (property , dbref , callback , dbRefProxyHandler );
1981
- }
1982
-
1983
- if (property .isDocumentReference ()) {
1984
-
1985
- return (T ) dbRefResolver .resolveReference (property ,
1986
- new DocumentReferenceSource (accessor .getDocument (), accessor .get (property )), referenceLookupDelegate ,
1987
- context ::convert );
1964
+ return (T ) readAssociation (property .getRequiredAssociation (), accessor , dbRefProxyHandler , callback ,
1965
+ propertyContext );
1988
1966
}
1989
1967
1990
1968
if (property .isUnwrapped ()) {
@@ -1993,6 +1971,10 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1993
1971
mappingContext .getRequiredPersistentEntity (property ));
1994
1972
}
1995
1973
1974
+ if (!accessor .hasValue (property )) {
1975
+ return null ;
1976
+ }
1977
+
1996
1978
return super .getPropertyValue (property );
1997
1979
}
1998
1980
}
0 commit comments