@@ -3069,8 +3069,8 @@ public void resolvesCyclicDBRefCorrectly() {
3069
3069
assertThat (contentLoaded .dbrefMessage .id , is (messageLoaded .id ));
3070
3070
}
3071
3071
3072
- @ Test // DATAMONGO-1287
3073
- public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstrcutorArgument () {
3072
+ @ Test // DATAMONGO-1287, DATAMONGO-2004
3073
+ public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstructorArgument () {
3074
3074
3075
3075
Document docInCtor = new Document ();
3076
3076
docInCtor .id = "doc-in-ctor" ;
@@ -3083,7 +3083,7 @@ public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstr
3083
3083
3084
3084
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template .findOne (query (where ("id" ).is (source .id )),
3085
3085
DocumentWithLazyDBrefUsedInPresistenceConstructor .class );
3086
- assertThat (loaded .refToDocUsedInCtor , not ( instanceOf (LazyLoadingProxy .class ) ));
3086
+ assertThat (loaded .refToDocUsedInCtor , instanceOf (LazyLoadingProxy .class ));
3087
3087
assertThat (loaded .refToDocNotUsedInCtor , nullValue ());
3088
3088
}
3089
3089
@@ -3106,8 +3106,8 @@ public void shouldNotReuseLazyLoadedDBRefWhenTypeUsedInPersistenceConstrcutorBut
3106
3106
assertThat (loaded .refToDocUsedInCtor , nullValue ());
3107
3107
}
3108
3108
3109
- @ Test // DATAMONGO-1287
3110
- public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstrcutor () {
3109
+ @ Test // DATAMONGO-1287, DATAMONGO-2004
3110
+ public void shouldRespectParameterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstructor () {
3111
3111
3112
3112
Document docInCtor = new Document ();
3113
3113
docInCtor .id = "doc-in-ctor" ;
@@ -3125,7 +3125,7 @@ public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedIn
3125
3125
3126
3126
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template .findOne (query (where ("id" ).is (source .id )),
3127
3127
DocumentWithLazyDBrefUsedInPresistenceConstructor .class );
3128
- assertThat (loaded .refToDocUsedInCtor , not ( instanceOf (LazyLoadingProxy .class ) ));
3128
+ assertThat (loaded .refToDocUsedInCtor , instanceOf (LazyLoadingProxy .class ));
3129
3129
assertThat (loaded .refToDocNotUsedInCtor , instanceOf (LazyLoadingProxy .class ));
3130
3130
}
3131
3131
@@ -3384,6 +3384,73 @@ public void shouldFetchMapOfLazyReferencesCorrectly() {
3384
3384
assertThat (target .lazyDbRefAnnotatedMap .values (), contains (two , one ));
3385
3385
}
3386
3386
3387
+ @ Test // DATAMONGO-2004
3388
+ public void shouldFetchLazyReferenceWithConstructorCreationCorrectly () {
3389
+
3390
+ Sample one = new Sample ("1" , "jon snow" );
3391
+
3392
+ template .save (one );
3393
+
3394
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , one ,
3395
+ null , null );
3396
+
3397
+ template .save (source );
3398
+
3399
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3400
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3401
+
3402
+ assertThat (target .lazyDbRefProperty , instanceOf (LazyLoadingProxy .class ));
3403
+ assertThat (target .lazyDbRefProperty , is (one ));
3404
+ }
3405
+
3406
+ @ Test // DATAMONGO-2004
3407
+ public void shouldFetchMapOfLazyReferencesWithConstructorCreationCorrectly () {
3408
+
3409
+ Sample one = new Sample ("1" , "jon snow" );
3410
+ Sample two = new Sample ("2" , "tyrion lannister" );
3411
+
3412
+ template .save (one );
3413
+ template .save (two );
3414
+
3415
+ Map <String , Sample > map = new LinkedHashMap <>();
3416
+ map .put ("tyrion" , two );
3417
+ map .put ("jon" , one );
3418
+
3419
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , null ,
3420
+ null , map );
3421
+
3422
+ template .save (source );
3423
+
3424
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3425
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3426
+
3427
+ assertThat (target .lazyDbRefAnnotatedMap , instanceOf (LazyLoadingProxy .class ));
3428
+ assertThat (target .lazyDbRefAnnotatedMap .values (), contains (two , one ));
3429
+ }
3430
+
3431
+ @ Test // DATAMONGO-2004
3432
+ public void shouldFetchListOfLazyReferencesWithConstructorCreationCorrectly () {
3433
+
3434
+ Sample one = new Sample ("1" , "jon snow" );
3435
+ Sample two = new Sample ("2" , "tyrion lannister" );
3436
+
3437
+ template .save (one );
3438
+ template .save (two );
3439
+
3440
+ List <Sample > list = Arrays .asList (two , one );
3441
+
3442
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , null ,
3443
+ list , null );
3444
+
3445
+ template .save (source );
3446
+
3447
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3448
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3449
+
3450
+ assertThat (target .lazyDbRefAnnotatedList , instanceOf (LazyLoadingProxy .class ));
3451
+ assertThat (target .lazyDbRefAnnotatedList , contains (two , one ));
3452
+ }
3453
+
3387
3454
@ Test // DATAMONGO-1513
3388
3455
@ DirtiesContext
3389
3456
public void populatesIdsAddedByEventListener () {
@@ -3590,6 +3657,29 @@ static class DocumentWithDBRefCollection {
3590
3657
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) public Map <String , Sample > lazyDbRefAnnotatedMap ;
3591
3658
}
3592
3659
3660
+ @ Data
3661
+ static class DocumentWithLazyDBRefsAndConstructorCreation {
3662
+
3663
+ @ Id public final String id ;
3664
+
3665
+ public DocumentWithLazyDBRefsAndConstructorCreation (String id , Sample lazyDbRefProperty ,
3666
+ List <Sample > lazyDbRefAnnotatedList , Map <String , Sample > lazyDbRefAnnotatedMap ) {
3667
+ this .id = id ;
3668
+ this .lazyDbRefProperty = lazyDbRefProperty ;
3669
+ this .lazyDbRefAnnotatedList = lazyDbRefAnnotatedList ;
3670
+ this .lazyDbRefAnnotatedMap = lazyDbRefAnnotatedMap ;
3671
+ }
3672
+
3673
+ @ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) //
3674
+ public final Sample lazyDbRefProperty ;
3675
+
3676
+ @ Field ("lazy_db_ref_list" ) @ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) //
3677
+ public final List <Sample > lazyDbRefAnnotatedList ;
3678
+
3679
+ @ Field ("lazy_db_ref_map" ) @ org .springframework .data .mongodb .core .mapping .DBRef (
3680
+ lazy = true ) public final Map <String , Sample > lazyDbRefAnnotatedMap ;
3681
+ }
3682
+
3593
3683
@ EqualsAndHashCode
3594
3684
static class DocumentWithCollection {
3595
3685
0 commit comments