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