Skip to content

Commit dd65bf0

Browse files
Add document reference to MongoPersistentProperty
1 parent 8cfb002 commit dd65bf0

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ private Object createLazyLoadingProxy(MongoPersistentProperty property, Object s
5656

5757
protected boolean isLazyReference(MongoPersistentProperty property) {
5858

59-
if (property.findAnnotation(DocumentReference.class) != null) {
60-
return property.findAnnotation(DocumentReference.class).lazy();
59+
if (property.isDocumentReference()) {
60+
return property.getDocumentReference().lazy();
6161
}
6262

6363
return property.getDBRef() != null && property.getDBRef().lazy();

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@
6363
import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider;
6464
import org.springframework.data.mongodb.CodecRegistryProvider;
6565
import org.springframework.data.mongodb.MongoDatabaseFactory;
66-
import org.springframework.data.mongodb.core.mapping.DocumentPointer;
67-
import org.springframework.data.mongodb.core.mapping.DocumentReference;
6866
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
6967
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
68+
import org.springframework.data.mongodb.core.mapping.DocumentPointer;
7069
import org.springframework.data.mongodb.core.mapping.Unwrapped;
7170
import org.springframework.data.mongodb.core.mapping.Unwrapped.OnEmpty;
7271
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
@@ -365,7 +364,7 @@ private <S extends Object> S read(ConversionContext context, MongoPersistentEnti
365364
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
366365
DocumentAccessor documentAccessor = new DocumentAccessor(bson);
367366

368-
if (bson.get("_id") != null) {
367+
if(bson.get("_id") != null) {
369368

370369
Object existing = context.getPath().getPathItem(bson.get("_id"), entity.getCollection(), entity.getType());
371370
if (existing != null) {
@@ -459,8 +458,7 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
459458
callback = getDbRefResolverCallback(context, documentAccessor, evaluator);
460459
}
461460

462-
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context,
463-
evaluator);
461+
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context, evaluator);
464462
continue;
465463
}
466464

@@ -487,8 +485,7 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
487485
callback = getDbRefResolverCallback(context, documentAccessor, evaluator);
488486
}
489487

490-
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context,
491-
evaluator);
488+
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context, evaluator);
492489
continue;
493490
}
494491

@@ -504,8 +501,7 @@ private DbRefResolverCallback getDbRefResolverCallback(ConversionContext context
504501
}
505502

506503
private void readAssociation(Association<MongoPersistentProperty> association, PersistentPropertyAccessor<?> accessor,
507-
DocumentAccessor documentAccessor, DbRefProxyHandler handler, DbRefResolverCallback callback,
508-
ConversionContext context, SpELExpressionEvaluator evaluator) {
504+
DocumentAccessor documentAccessor, DbRefProxyHandler handler, DbRefResolverCallback callback, ConversionContext context, SpELExpressionEvaluator evaluator) {
509505

510506
MongoPersistentProperty property = association.getInverse();
511507
final Object value = documentAccessor.get(property);
@@ -514,7 +510,7 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
514510
return;
515511
}
516512

517-
if (property.isAnnotationPresent(DocumentReference.class)) {
513+
if (property.isDocumentReference()) {
518514

519515
// quite unusual but sounds like worth having?
520516

@@ -867,15 +863,15 @@ protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty proper
867863
if (conversions.isSimpleType(key.getClass())) {
868864

869865
String simpleKey = prepareMapKey(key.toString());
870-
if (property.isDbReference()) {
866+
if(property.isDbReference()) {
871867
document.put(simpleKey, value != null ? createDBRef(value, property) : null);
872868
} else {
873869
if (conversionService.canConvert(value.getClass(), DocumentPointer.class)) {
874870
document.put(simpleKey, conversionService.convert(value, DocumentPointer.class).getPointer());
875871
} else {
876872
// just take the id as a reference
877-
document.put(simpleKey, mappingContext.getPersistentEntity(property.getAssociationTargetType())
878-
.getIdentifierAccessor(value).getIdentifier());
873+
document.put(simpleKey, mappingContext.getPersistentEntity(property.getAssociationTargetType()).getIdentifierAccessor(value)
874+
.getIdentifier());
879875
}
880876
}
881877

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ private ReferenceCollection computeReferenceContext(MongoPersistentProperty prop
111111

112112
Document ref = (Document) value;
113113

114-
if (property.isAnnotationPresent(DocumentReference.class)) {
114+
if (property.isDocumentReference()) {
115115

116116
ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
117-
DocumentReference documentReference = property.getRequiredAnnotation(DocumentReference.class);
117+
DocumentReference documentReference = property.getDocumentReference();
118118

119119
String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext,
120120
() -> ref.get("db", String.class));
@@ -128,10 +128,10 @@ private ReferenceCollection computeReferenceContext(MongoPersistentProperty prop
128128
mappingContext.get().getPersistentEntity(property.getAssociationTargetType()).getCollection()));
129129
}
130130

131-
if (property.isAnnotationPresent(DocumentReference.class)) {
131+
if (property.isDocumentReference()) {
132132

133133
ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
134-
DocumentReference documentReference = property.getRequiredAnnotation(DocumentReference.class);
134+
DocumentReference documentReference = property.getDocumentReference();
135135

136136
String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext, () -> null);
137137
String targetCollection = parseValueOrGet(documentReference.collection(), bindingContext,
@@ -188,7 +188,7 @@ EvaluationContext evaluationContextFor(MongoPersistentProperty property, Object
188188

189189
DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object value, SpELContext spELContext) {
190190

191-
DocumentReference documentReference = property.getRequiredAnnotation(DocumentReference.class);
191+
DocumentReference documentReference = property.getDocumentReference();
192192
String lookup = documentReference.lookup();
193193

194194
Document sort = parseValueOrGet(documentReference.sort(), bindingContext(property, value, spELContext), () -> null);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ public boolean isDbReference() {
231231
return isAnnotationPresent(DBRef.class);
232232
}
233233

234+
/*
235+
* (non-Javadoc)
236+
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#isDocumentReference()
237+
*/
238+
@Override
239+
public boolean isDocumentReference() {
240+
return isAnnotationPresent(DocumentReference.class);
241+
}
242+
234243
/*
235244
* (non-Javadoc)
236245
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#getDBRef()
@@ -240,6 +249,16 @@ public DBRef getDBRef() {
240249
return findAnnotation(DBRef.class);
241250
}
242251

252+
/*
253+
* (non-Javadoc)
254+
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#getDocumentReference()
255+
*/
256+
@Nullable
257+
@Override
258+
public DocumentReference getDocumentReference() {
259+
return findAnnotation(DocumentReference.class);
260+
}
261+
243262
/*
244263
* (non-Javadoc)
245264
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#isLanguageProperty()

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoPersistentProperty.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public interface MongoPersistentProperty extends PersistentProperty<MongoPersist
6262
*/
6363
boolean isDbReference();
6464

65+
/**
66+
* Returns whether the property is a {@link DocumentReference}. If this returns {@literal true} you can expect
67+
* {@link #getDocumentReference()} to return an non-{@literal null} value.
68+
*
69+
* @return
70+
* @since 3.3
71+
*/
72+
boolean isDocumentReference();
73+
6574
/**
6675
* Returns whether the property is explicitly marked as an identifier property of the owning {@link PersistentEntity}.
6776
* A property is an explicit id property if it is annotated with @see {@link Id}.
@@ -105,6 +114,16 @@ public interface MongoPersistentProperty extends PersistentProperty<MongoPersist
105114
@Nullable
106115
DBRef getDBRef();
107116

117+
/**
118+
* Returns the {@link DocumentReference} if the property is a reference.
119+
*
120+
* @see #isDocumentReference()
121+
* @return {@literal null} if not present.
122+
* @since 3.3
123+
*/
124+
@Nullable
125+
DocumentReference getDocumentReference();
126+
108127
/**
109128
* Returns whether property access shall be used for reading the property value. This means it will use the getter
110129
* instead of field access.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/UnwrappedMongoPersistentProperty.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public boolean isDbReference() {
6868
return delegate.isDbReference();
6969
}
7070

71+
@Override
72+
public boolean isDocumentReference() {
73+
return delegate.isDocumentReference();
74+
}
75+
7176
@Override
7277
public boolean isExplicitIdProperty() {
7378
return delegate.isExplicitIdProperty();
@@ -94,6 +99,12 @@ public DBRef getDBRef() {
9499
return delegate.getDBRef();
95100
}
96101

102+
@Override
103+
@Nullable
104+
public DocumentReference getDocumentReference() {
105+
return delegate.getDocumentReference();
106+
}
107+
97108
@Override
98109
public boolean usePropertyAccess() {
99110
return delegate.usePropertyAccess();

0 commit comments

Comments
 (0)