Skip to content

Commit c545c85

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2004 - Support lazy DBRef resolution through constructor creation of the enclosing entity.
We now respect eager/lazy loading preferences of the annotated association property when the enclosing entity is created through its constructor and the reference is passed as constructor argument. Previously, we eagerly resolved DBRefs and passed the resolved value to the constructor. Original Pull Request: spring-projects#571
1 parent 1b7678a commit c545c85

File tree

3 files changed

+154
-10
lines changed

3 files changed

+154
-10
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DocumentAccessor.java

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public DocumentAccessor(Bson document) {
5858
this.document = document;
5959
}
6060

61+
/**
62+
* @return the underlying {@link Bson document}.
63+
* @since 2.1
64+
*/
65+
public Bson getDocument() {
66+
return this.document;
67+
}
68+
6169
/**
6270
* Puts the given value into the backing {@link Document} based on the coordinates defined through the given
6371
* {@link MongoPersistentProperty}. By default this will be the plain field name. But field names might also consist

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+50-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ private <S extends Object> S read(TypeInformation<S> type, @Nullable Bson bson,
255255
private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(MongoPersistentEntity<?> entity,
256256
Bson source, DefaultSpELExpressionEvaluator evaluator, ObjectPath path) {
257257

258-
MongoDbPropertyValueProvider provider = new MongoDbPropertyValueProvider(source, evaluator, path);
258+
AssociationAwareMongoDbPropertyValueProvider provider = new AssociationAwareMongoDbPropertyValueProvider(source,
259+
evaluator, path);
259260
PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>(
260261
entity, provider, path.getCurrentObject());
261262

@@ -1273,9 +1274,9 @@ private Object removeTypeInfo(Object object, boolean recursively) {
12731274
*/
12741275
class MongoDbPropertyValueProvider implements PropertyValueProvider<MongoPersistentProperty> {
12751276

1276-
private final DocumentAccessor source;
1277-
private final SpELExpressionEvaluator evaluator;
1278-
private final ObjectPath path;
1277+
final DocumentAccessor source;
1278+
final SpELExpressionEvaluator evaluator;
1279+
final ObjectPath path;
12791280

12801281
/**
12811282
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1320,6 +1321,7 @@ public MongoDbPropertyValueProvider(DocumentAccessor accessor, SpELExpressionEva
13201321
* @see org.springframework.data.convert.PropertyValueProvider#getPropertyValue(org.springframework.data.mapping.PersistentProperty)
13211322
*/
13221323
@Nullable
1324+
@SuppressWarnings("unchecked")
13231325
public <T> T getPropertyValue(MongoPersistentProperty property) {
13241326

13251327
String expression = property.getSpelExpression();
@@ -1333,6 +1335,50 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
13331335
}
13341336
}
13351337

1338+
/**
1339+
* {@link PropertyValueProvider} that is aware of {@link MongoPersistentProperty#isAssociation()} and that delegates
1340+
* resolution to {@link DbRefResolver}.
1341+
*
1342+
* @author Mark Paluch
1343+
* @since 2.1
1344+
*/
1345+
class AssociationAwareMongoDbPropertyValueProvider extends MongoDbPropertyValueProvider {
1346+
1347+
/**
1348+
* Creates a new {@link AssociationAwareMongoDbPropertyValueProvider} for the given source,
1349+
* {@link SpELExpressionEvaluator} and {@link ObjectPath}.
1350+
*
1351+
* @param source must not be {@literal null}.
1352+
* @param evaluator must not be {@literal null}.
1353+
* @param path must not be {@literal null}.
1354+
*/
1355+
public AssociationAwareMongoDbPropertyValueProvider(Bson source, SpELExpressionEvaluator evaluator,
1356+
ObjectPath path) {
1357+
super(source, evaluator, path);
1358+
}
1359+
1360+
/*
1361+
* (non-Javadoc)
1362+
* @see org.springframework.data.convert.PropertyValueProvider#getPropertyValue(org.springframework.data.mapping.PersistentProperty)
1363+
*/
1364+
@Nullable
1365+
@SuppressWarnings("unchecked")
1366+
public <T> T getPropertyValue(MongoPersistentProperty property) {
1367+
1368+
T value = super.getPropertyValue(property);
1369+
1370+
if (value == null || !property.isAssociation()) {
1371+
return value;
1372+
}
1373+
1374+
DbRefResolverCallback callback = new DefaultDbRefResolverCallback(source.getDocument(), path, evaluator,
1375+
MappingMongoConverter.this);
1376+
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
1377+
1378+
return (T) dbRefResolver.resolveDbRef(property, dbref, callback, dbRefProxyHandler);
1379+
}
1380+
}
1381+
13361382
/**
13371383
* Extension of {@link SpELExpressionParameterValueProvider} to recursively trigger value conversion on the raw
13381384
* resolved SpEL value.

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+96-6
Original file line numberDiff line numberDiff line change
@@ -3069,8 +3069,8 @@ public void resolvesCyclicDBRefCorrectly() {
30693069
assertThat(contentLoaded.dbrefMessage.id, is(messageLoaded.id));
30703070
}
30713071

3072-
@Test // DATAMONGO-1287
3073-
public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstrcutorArgument() {
3072+
@Test // DATAMONGO-1287, DATAMONGO-2004
3073+
public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstructorArgument() {
30743074

30753075
Document docInCtor = new Document();
30763076
docInCtor.id = "doc-in-ctor";
@@ -3083,7 +3083,7 @@ public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstr
30833083

30843084
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template.findOne(query(where("id").is(source.id)),
30853085
DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
3086-
assertThat(loaded.refToDocUsedInCtor, not(instanceOf(LazyLoadingProxy.class)));
3086+
assertThat(loaded.refToDocUsedInCtor, instanceOf(LazyLoadingProxy.class));
30873087
assertThat(loaded.refToDocNotUsedInCtor, nullValue());
30883088
}
30893089

@@ -3106,8 +3106,8 @@ public void shouldNotReuseLazyLoadedDBRefWhenTypeUsedInPersistenceConstrcutorBut
31063106
assertThat(loaded.refToDocUsedInCtor, nullValue());
31073107
}
31083108

3109-
@Test // DATAMONGO-1287
3110-
public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstrcutor() {
3109+
@Test // DATAMONGO-1287, DATAMONGO-2004
3110+
public void shouldRespectParameterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstructor() {
31113111

31123112
Document docInCtor = new Document();
31133113
docInCtor.id = "doc-in-ctor";
@@ -3125,7 +3125,7 @@ public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedIn
31253125

31263126
DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template.findOne(query(where("id").is(source.id)),
31273127
DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
3128-
assertThat(loaded.refToDocUsedInCtor, not(instanceOf(LazyLoadingProxy.class)));
3128+
assertThat(loaded.refToDocUsedInCtor, instanceOf(LazyLoadingProxy.class));
31293129
assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class));
31303130
}
31313131

@@ -3384,6 +3384,73 @@ public void shouldFetchMapOfLazyReferencesCorrectly() {
33843384
assertThat(target.lazyDbRefAnnotatedMap.values(), contains(two, one));
33853385
}
33863386

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+
33873454
@Test // DATAMONGO-1513
33883455
@DirtiesContext
33893456
public void populatesIdsAddedByEventListener() {
@@ -3590,6 +3657,29 @@ static class DocumentWithDBRefCollection {
35903657
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) public Map<String, Sample> lazyDbRefAnnotatedMap;
35913658
}
35923659

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+
35933683
@EqualsAndHashCode
35943684
static class DocumentWithCollection {
35953685

0 commit comments

Comments
 (0)