@@ -2935,8 +2935,8 @@ public void resolvesCyclicDBRefCorrectly() {
2935
2935
assertThat (contentLoaded .dbrefMessage .id , is (messageLoaded .id ));
2936
2936
}
2937
2937
2938
- @ Test // DATAMONGO-1287
2939
- public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstrcutorArgument () {
2938
+ @ Test // DATAMONGO-1287, DATAMONGO-2004
2939
+ public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstructorArgument () {
2940
2940
2941
2941
Document docInCtor = new Document ();
2942
2942
docInCtor .id = "doc-in-ctor" ;
@@ -2949,7 +2949,7 @@ public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstr
2949
2949
2950
2950
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template .findOne (query (where ("id" ).is (source .id )),
2951
2951
DocumentWithLazyDBrefUsedInPresistenceConstructor .class );
2952
- assertThat (loaded .refToDocUsedInCtor , not ( instanceOf (LazyLoadingProxy .class ) ));
2952
+ assertThat (loaded .refToDocUsedInCtor , instanceOf (LazyLoadingProxy .class ));
2953
2953
assertThat (loaded .refToDocNotUsedInCtor , nullValue ());
2954
2954
}
2955
2955
@@ -2972,8 +2972,8 @@ public void shouldNotReuseLazyLoadedDBRefWhenTypeUsedInPersistenceConstrcutorBut
2972
2972
assertThat (loaded .refToDocUsedInCtor , nullValue ());
2973
2973
}
2974
2974
2975
- @ Test // DATAMONGO-1287
2976
- public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstrcutor () {
2975
+ @ Test // DATAMONGO-1287, DATAMONGO-2004
2976
+ public void shouldRespectParameterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstructor () {
2977
2977
2978
2978
Document docInCtor = new Document ();
2979
2979
docInCtor .id = "doc-in-ctor" ;
@@ -2991,7 +2991,7 @@ public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedIn
2991
2991
2992
2992
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template .findOne (query (where ("id" ).is (source .id )),
2993
2993
DocumentWithLazyDBrefUsedInPresistenceConstructor .class );
2994
- assertThat (loaded .refToDocUsedInCtor , not ( instanceOf (LazyLoadingProxy .class ) ));
2994
+ assertThat (loaded .refToDocUsedInCtor , instanceOf (LazyLoadingProxy .class ));
2995
2995
assertThat (loaded .refToDocNotUsedInCtor , instanceOf (LazyLoadingProxy .class ));
2996
2996
}
2997
2997
@@ -3250,6 +3250,73 @@ public void shouldFetchMapOfLazyReferencesCorrectly() {
3250
3250
assertThat (target .lazyDbRefAnnotatedMap .values (), contains (two , one ));
3251
3251
}
3252
3252
3253
+ @ Test // DATAMONGO-2004
3254
+ public void shouldFetchLazyReferenceWithConstructorCreationCorrectly () {
3255
+
3256
+ Sample one = new Sample ("1" , "jon snow" );
3257
+
3258
+ template .save (one );
3259
+
3260
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , one ,
3261
+ null , null );
3262
+
3263
+ template .save (source );
3264
+
3265
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3266
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3267
+
3268
+ assertThat (target .lazyDbRefProperty , instanceOf (LazyLoadingProxy .class ));
3269
+ assertThat (target .lazyDbRefProperty , is (one ));
3270
+ }
3271
+
3272
+ @ Test // DATAMONGO-2004
3273
+ public void shouldFetchMapOfLazyReferencesWithConstructorCreationCorrectly () {
3274
+
3275
+ Sample one = new Sample ("1" , "jon snow" );
3276
+ Sample two = new Sample ("2" , "tyrion lannister" );
3277
+
3278
+ template .save (one );
3279
+ template .save (two );
3280
+
3281
+ Map <String , Sample > map = new LinkedHashMap <>();
3282
+ map .put ("tyrion" , two );
3283
+ map .put ("jon" , one );
3284
+
3285
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , null ,
3286
+ null , map );
3287
+
3288
+ template .save (source );
3289
+
3290
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3291
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3292
+
3293
+ assertThat (target .lazyDbRefAnnotatedMap , instanceOf (LazyLoadingProxy .class ));
3294
+ assertThat (target .lazyDbRefAnnotatedMap .values (), contains (two , one ));
3295
+ }
3296
+
3297
+ @ Test // DATAMONGO-2004
3298
+ public void shouldFetchListOfLazyReferencesWithConstructorCreationCorrectly () {
3299
+
3300
+ Sample one = new Sample ("1" , "jon snow" );
3301
+ Sample two = new Sample ("2" , "tyrion lannister" );
3302
+
3303
+ template .save (one );
3304
+ template .save (two );
3305
+
3306
+ List <Sample > list = Arrays .asList (two , one );
3307
+
3308
+ DocumentWithLazyDBRefsAndConstructorCreation source = new DocumentWithLazyDBRefsAndConstructorCreation (null , null ,
3309
+ list , null );
3310
+
3311
+ template .save (source );
3312
+
3313
+ DocumentWithLazyDBRefsAndConstructorCreation target = template .findOne (query (where ("id" ).is (source .id )),
3314
+ DocumentWithLazyDBRefsAndConstructorCreation .class );
3315
+
3316
+ assertThat (target .lazyDbRefAnnotatedList , instanceOf (LazyLoadingProxy .class ));
3317
+ assertThat (target .lazyDbRefAnnotatedList , contains (two , one ));
3318
+ }
3319
+
3253
3320
@ Test // DATAMONGO-1513
3254
3321
@ DirtiesContext
3255
3322
public void populatesIdsAddedByEventListener () {
@@ -3457,6 +3524,29 @@ static class DocumentWithDBRefCollection {
3457
3524
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) public Map <String , Sample > lazyDbRefAnnotatedMap ;
3458
3525
}
3459
3526
3527
+ @ Data
3528
+ static class DocumentWithLazyDBRefsAndConstructorCreation {
3529
+
3530
+ @ Id public final String id ;
3531
+
3532
+ public DocumentWithLazyDBRefsAndConstructorCreation (String id , Sample lazyDbRefProperty ,
3533
+ List <Sample > lazyDbRefAnnotatedList , Map <String , Sample > lazyDbRefAnnotatedMap ) {
3534
+ this .id = id ;
3535
+ this .lazyDbRefProperty = lazyDbRefProperty ;
3536
+ this .lazyDbRefAnnotatedList = lazyDbRefAnnotatedList ;
3537
+ this .lazyDbRefAnnotatedMap = lazyDbRefAnnotatedMap ;
3538
+ }
3539
+
3540
+ @ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) //
3541
+ public final Sample lazyDbRefProperty ;
3542
+
3543
+ @ Field ("lazy_db_ref_list" ) @ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) //
3544
+ public final List <Sample > lazyDbRefAnnotatedList ;
3545
+
3546
+ @ Field ("lazy_db_ref_map" ) @ org .springframework .data .mongodb .core .mapping .DBRef (
3547
+ lazy = true ) public final Map <String , Sample > lazyDbRefAnnotatedMap ;
3548
+ }
3549
+
3460
3550
@ EqualsAndHashCode
3461
3551
static class DocumentWithCollection {
3462
3552
0 commit comments