36
36
import org .junit .runner .RunWith ;
37
37
import org .mockito .Mock ;
38
38
import org .mockito .runners .MockitoJUnitRunner ;
39
+ import org .springframework .data .annotation .AccessType ;
40
+ import org .springframework .data .annotation .AccessType .Type ;
39
41
import org .springframework .data .annotation .Id ;
40
42
import org .springframework .data .annotation .PersistenceConstructor ;
41
43
import org .springframework .data .mapping .PropertyPath ;
46
48
import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
47
49
import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
48
50
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
51
+ import org .springframework .test .util .ReflectionTestUtils ;
49
52
import org .springframework .util .SerializationUtils ;
50
53
51
54
import com .mongodb .BasicDBObject ;
@@ -494,13 +497,17 @@ public void shouldBeAbleToStoreNestedReferencesToSelf() {
494
497
assertThat (found .nested .reference , is (found ));
495
498
}
496
499
500
+ /**
501
+ * @see DATAMONGO-1012
502
+ */
497
503
@ Test
498
- public void testname () {
504
+ public void shouldEagerlyResolveIdPropertyWithFieldAccess () {
499
505
500
506
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (ClassWithLazyDbRefs .class );
501
507
MongoPersistentProperty property = entity .getPersistentProperty ("dbRefToConcreteType" );
502
508
503
- Object dbRef = converter .toDBRef (new LazyDbRefTarget (new ObjectId ().toString ()), property );
509
+ String idValue = new ObjectId ().toString ();
510
+ DBRef dbRef = converter .toDBRef (new LazyDbRefTarget (idValue ), property );
504
511
505
512
DBObject object = new BasicDBObject ("dbRefToConcreteType" , dbRef );
506
513
@@ -510,6 +517,28 @@ public void testname() {
510
517
MongoPersistentProperty idProperty = mappingContext .getPersistentEntity (LazyDbRefTarget .class ).getIdProperty ();
511
518
512
519
assertThat (wrapper .getProperty (idProperty ), is (notNullValue ()));
520
+ assertProxyIsResolved (result .dbRefToConcreteType , false );
521
+ }
522
+
523
+ /**
524
+ * @see DATAMONGO-1012
525
+ */
526
+ @ Test
527
+ public void shouldNotEagerlyResolveIdPropertyWithPropertyAccess () {
528
+
529
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (ClassWithLazyDbRefs .class );
530
+ MongoPersistentProperty property = entity .getPersistentProperty ("dbRefToConcreteTypeWithPropertyAccess" );
531
+
532
+ String idValue = new ObjectId ().toString ();
533
+ DBRef dbRef = converter .toDBRef (new LazyDbRefTargetPropertyAccess (idValue ), property );
534
+
535
+ DBObject object = new BasicDBObject ("dbRefToConcreteTypeWithPropertyAccess" , dbRef );
536
+
537
+ ClassWithLazyDbRefs result = converter .read (ClassWithLazyDbRefs .class , object );
538
+
539
+ LazyDbRefTargetPropertyAccess proxy = result .dbRefToConcreteTypeWithPropertyAccess ;
540
+ assertThat (ReflectionTestUtils .getField (proxy , "id" ), is (nullValue ()));
541
+ assertProxyIsResolved (proxy , false );
513
542
}
514
543
515
544
private Object transport (Object result ) {
@@ -534,6 +563,7 @@ static class ClassWithLazyDbRefs {
534
563
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) List <LazyDbRefTarget > dbRefToInterface ;
535
564
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) ArrayList <LazyDbRefTarget > dbRefToConcreteCollection ;
536
565
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) LazyDbRefTarget dbRefToConcreteType ;
566
+ @ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) LazyDbRefTargetPropertyAccess dbRefToConcreteTypeWithPropertyAccess ;
537
567
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) LazyDbRefTargetWithPeristenceConstructor dbRefToConcreteTypeWithPersistenceConstructor ;
538
568
@ org .springframework .data .mongodb .core .mapping .DBRef (lazy = true ) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor ;
539
569
}
@@ -574,6 +604,21 @@ public String getValue() {
574
604
}
575
605
}
576
606
607
+ static class LazyDbRefTargetPropertyAccess implements Serializable {
608
+
609
+ private static final long serialVersionUID = 1L ;
610
+
611
+ @ Id @ AccessType (Type .PROPERTY ) String id ;
612
+
613
+ public LazyDbRefTargetPropertyAccess (String id ) {
614
+ this .id = id ;
615
+ }
616
+
617
+ public String getId () {
618
+ return id ;
619
+ }
620
+ }
621
+
577
622
@ SuppressWarnings ("serial" )
578
623
static class LazyDbRefTargetWithPeristenceConstructor extends LazyDbRefTarget {
579
624
0 commit comments