Skip to content

Commit 5a87dec

Browse files
christophstroblmp911de
authored andcommitted
Rename Embedded annotation to Unwrapped.
The meaning of embedding a Document in MongoDB is different compared to column based stores. Typically the term is used for a Document in Document approach and not for flattening out a values into the enclosing Document. Closes: spring-projects#3600 Original pull request: spring-projects#3604.
1 parent f455640 commit 5a87dec

26 files changed

+421
-422
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@
6262
import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider;
6363
import org.springframework.data.mongodb.CodecRegistryProvider;
6464
import org.springframework.data.mongodb.MongoDatabaseFactory;
65-
import org.springframework.data.mongodb.core.mapping.Embedded;
66-
import org.springframework.data.mongodb.core.mapping.Embedded.OnEmpty;
6765
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
6866
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
67+
import org.springframework.data.mongodb.core.mapping.Unwrapped;
68+
import org.springframework.data.mongodb.core.mapping.Unwrapped.OnEmpty;
6969
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
7070
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
7171
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
@@ -447,10 +447,10 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
447447
continue;
448448
}
449449

450-
if (prop.isEmbedded()) {
450+
if (prop.isUnwrapped()) {
451451

452452
accessor.setProperty(prop,
453-
readEmbedded(context, documentAccessor, prop, mappingContext.getRequiredPersistentEntity(prop)));
453+
readUnwrapped(context, documentAccessor, prop, mappingContext.getRequiredPersistentEntity(prop)));
454454
continue;
455455
}
456456

@@ -500,17 +500,17 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
500500
}
501501

502502
@Nullable
503-
private Object readEmbedded(ConversionContext context, DocumentAccessor documentAccessor,
503+
private Object readUnwrapped(ConversionContext context, DocumentAccessor documentAccessor,
504504
MongoPersistentProperty prop,
505-
MongoPersistentEntity<?> embeddedEntity) {
505+
MongoPersistentEntity<?> unwrappedEntity) {
506506

507-
if (prop.findAnnotation(Embedded.class).onEmpty().equals(OnEmpty.USE_EMPTY)) {
508-
return read(context, embeddedEntity, (Document) documentAccessor.getDocument());
507+
if (prop.findAnnotation(Unwrapped.class).onEmpty().equals(OnEmpty.USE_EMPTY)) {
508+
return read(context, unwrappedEntity, (Document) documentAccessor.getDocument());
509509
}
510510

511-
for (MongoPersistentProperty persistentProperty : embeddedEntity) {
511+
for (MongoPersistentProperty persistentProperty : unwrappedEntity) {
512512
if (documentAccessor.hasValue(persistentProperty)) {
513-
return read(context, embeddedEntity, (Document) documentAccessor.getDocument());
513+
return read(context, unwrappedEntity, (Document) documentAccessor.getDocument());
514514
}
515515
}
516516
return null;
@@ -680,7 +680,7 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
680680
TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
681681
TypeInformation<?> type = prop.getTypeInformation();
682682

683-
if (prop.isEmbedded()) {
683+
if (prop.isUnwrapped()) {
684684

685685
Document target = new Document();
686686
writeInternal(obj, target, mappingContext.getPersistentEntity(prop));

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Document getMappedObject(Bson query, @Nullable MongoPersistentEntity<?> e
144144
Field field = createPropertyField(entity, key, mappingContext);
145145

146146
// TODO: move to dedicated method
147-
if (field.getProperty() != null && field.getProperty().isEmbedded()) {
147+
if (field.getProperty() != null && field.getProperty().isUnwrapped()) {
148148

149149
Object theNestedObject = BsonUtils.get(query, key);
150150
Document mappedValue = (Document) getMappedValue(field, theNestedObject);
@@ -189,13 +189,13 @@ public Document getMappedSort(Document sortObject, @Nullable MongoPersistentEnti
189189
return new Document();
190190
}
191191

192-
sortObject = filterEmbeddedObjects(sortObject, entity);
192+
sortObject = filterUnwrappedObjects(sortObject, entity);
193193

194194
Document mappedSort = new Document();
195195
for (Map.Entry<String, Object> entry : BsonUtils.asMap(sortObject).entrySet()) {
196196

197197
Field field = createPropertyField(entity, entry.getKey(), mappingContext);
198-
if (field.getProperty() != null && field.getProperty().isEmbedded()) {
198+
if (field.getProperty() != null && field.getProperty().isUnwrapped()) {
199199
continue;
200200
}
201201

@@ -219,7 +219,7 @@ public Document getMappedFields(Document fieldsObject, @Nullable MongoPersistent
219219

220220
Assert.notNull(fieldsObject, "FieldsObject must not be null!");
221221

222-
fieldsObject = filterEmbeddedObjects(fieldsObject, entity);
222+
fieldsObject = filterUnwrappedObjects(fieldsObject, entity);
223223

224224
Document mappedFields = getMappedObject(fieldsObject, entity);
225225
mapMetaAttributes(mappedFields, entity, MetaMapping.FORCE);
@@ -241,7 +241,7 @@ private void mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<
241241
}
242242
}
243243

244-
private Document filterEmbeddedObjects(Document fieldsObject, @Nullable MongoPersistentEntity<?> entity) {
244+
private Document filterUnwrappedObjects(Document fieldsObject, @Nullable MongoPersistentEntity<?> entity) {
245245

246246
if (fieldsObject.isEmpty() || entity == null) {
247247
return fieldsObject;
@@ -258,13 +258,13 @@ private Document filterEmbeddedObjects(Document fieldsObject, @Nullable MongoPer
258258
.getPersistentPropertyPath(path);
259259
MongoPersistentProperty property = mappingContext.getPersistentPropertyPath(path).getRequiredLeafProperty();
260260

261-
if (property.isEmbedded() && property.isEntity()) {
261+
if (property.isUnwrapped() && property.isEntity()) {
262262

263-
MongoPersistentEntity<?> embeddedEntity = mappingContext.getRequiredPersistentEntity(property);
263+
MongoPersistentEntity<?> unwrappedEntity = mappingContext.getRequiredPersistentEntity(property);
264264

265-
for (MongoPersistentProperty embedded : embeddedEntity) {
265+
for (MongoPersistentProperty unwrappedProperty : unwrappedEntity) {
266266

267-
DotPath dotPath = DotPath.from(persistentPropertyPath.toDotPath()).append(embedded.getName());
267+
DotPath dotPath = DotPath.from(persistentPropertyPath.toDotPath()).append(unwrappedProperty.getName());
268268
target.put(dotPath.toString(), field.getValue());
269269
}
270270

@@ -564,7 +564,7 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten
564564
@Nullable
565565
protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersistentEntity<?> entity) {
566566

567-
if (entity != null && entity.isEmbedded()) {
567+
if (entity != null && entity.isUnwrapped()) {
568568
return converter.convertToMongoType(source, entity);
569569
}
570570

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static boolean isUpdateObject(@Nullable Document updateObj) {
133133
@Override
134134
protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersistentEntity<?> entity) {
135135

136-
if(entity != null && entity.isEmbedded()) {
136+
if(entity != null && entity.isUnwrapped()) {
137137
return converter.convertToMongoType(source, entity);
138138
}
139139

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void potentiallyAddIndexForProperty(MongoPersistentEntity<?> root, Mongo
137137
try {
138138
if (persistentProperty.isEntity()) {
139139
indexes.addAll(resolveIndexForEntity(mappingContext.getPersistentEntity(persistentProperty),
140-
persistentProperty.isEmbedded() ? "" : persistentProperty.getFieldName(), Path.of(persistentProperty),
140+
persistentProperty.isUnwrapped() ? "" : persistentProperty.getFieldName(), Path.of(persistentProperty),
141141
root.getCollection(), guard));
142142
}
143143

@@ -187,7 +187,7 @@ private void guardAndPotentiallyAddIndexForProperty(MongoPersistentProperty pers
187187

188188
DotPath propertyDotPath = DotPath.from(dotPath);
189189

190-
if (!persistentProperty.isEmbedded()) {
190+
if (!persistentProperty.isUnwrapped()) {
191191
propertyDotPath = propertyDotPath.append(persistentProperty.getFieldName());
192192
}
193193

@@ -216,11 +216,11 @@ private List<IndexDefinitionHolder> createIndexDefinitionHolderForProperty(Strin
216216

217217
List<IndexDefinitionHolder> indices = new ArrayList<>(2);
218218

219-
if (persistentProperty.isEmbedded() && (persistentProperty.isAnnotationPresent(Indexed.class)
219+
if (persistentProperty.isUnwrapped() && (persistentProperty.isAnnotationPresent(Indexed.class)
220220
|| persistentProperty.isAnnotationPresent(HashIndexed.class)
221221
|| persistentProperty.isAnnotationPresent(GeoSpatialIndexed.class))) {
222222
throw new InvalidDataAccessApiUsageException(
223-
String.format("Index annotation not allowed on embedded object for path '%s'.", dotPath));
223+
String.format("Index annotation not allowed on unwrapped object for path '%s'.", dotPath));
224224
}
225225

226226
if (persistentProperty.isAnnotationPresent(Indexed.class)) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public MongoPersistentEntity<?> getPersistentEntity(MongoPersistentProperty pers
133133

134134
MongoPersistentEntity<?> entity = super.getPersistentEntity(persistentProperty);
135135

136-
if(entity == null || !persistentProperty.isEmbedded()) {
136+
if(entity == null || !persistentProperty.isUnwrapped()) {
137137
return entity;
138138
}
139139

140-
return new EmbeddedMongoPersistentEntity<>(entity, new EmbeddedEntityContext(persistentProperty));
140+
return new UnwrappedMongoPersistentEntity<>(entity, new UnwrapEntityContext(persistentProperty));
141141
}
142142
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ default boolean isSharded() {
9595
}
9696

9797
/**
98-
* @return {@literal true} if the entity should be embedded.
98+
* @return {@literal true} if the entity should be unwrapped.
9999
* @since 3.2
100100
*/
101-
default boolean isEmbedded() {
101+
default boolean isUnwrapped() {
102102
return false;
103103
}
104104

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ default boolean hasExplicitWriteTarget() {
124124
}
125125

126126
/**
127-
* @return {@literal true} if the property should be embedded.
127+
* @return {@literal true} if the property should be unwrapped.
128128
* @since 3.2
129129
*/
130-
default boolean isEmbedded() {
131-
return isEntity() && isAnnotationPresent(Embedded.class);
130+
default boolean isUnwrapped() {
131+
return isEntity() && isAnnotationPresent(Unwrapped.class);
132132
}
133133

134134
/**
@@ -145,7 +145,7 @@ enum PropertyToFieldNameConverter implements Converter<MongoPersistentProperty,
145145
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
146146
*/
147147
public String convert(MongoPersistentProperty source) {
148-
if (!source.isEmbedded()) {
148+
if (!source.isUnwrapped()) {
149149
return source.getFieldName();
150150
}
151151
return "";
+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
* @author Christoph Strobl
2020
* @since 3.2
2121
*/
22-
class EmbeddedEntityContext {
22+
class UnwrapEntityContext {
2323

2424
private final MongoPersistentProperty property;
2525

26-
public EmbeddedEntityContext(MongoPersistentProperty property) {
26+
public UnwrapEntityContext(MongoPersistentProperty property) {
2727
this.property = property;
2828
}
2929

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Embedded.java renamed to spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Unwrapped.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,35 @@
2626
import org.springframework.core.annotation.AliasFor;
2727

2828
/**
29-
* The annotation to configure a value object as embedded (flattened out) in the target document.
29+
* The annotation to configure a value object as flattened out in the target document.
3030
* <p />
3131
* Depending on the {@link OnEmpty value} of {@link #onEmpty()} the property is set to {@literal null} or an empty
32-
* instance in the case all embedded values are {@literal null} when reading from the result set.
32+
* instance in the case all unwrapped values are {@literal null} when reading from the result set.
3333
*
3434
* @author Christoph Strobl
3535
* @since 3.2
3636
*/
3737
@Documented
3838
@Retention(value = RetentionPolicy.RUNTIME)
3939
@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD })
40-
public @interface Embedded {
40+
public @interface Unwrapped {
4141

4242
/**
43-
* Set the load strategy for the embedded object if all contained fields yield {@literal null} values.
43+
* Set the load strategy for the unwrapped object if all contained fields yield {@literal null} values.
4444
* <p />
45-
* {@link Nullable @Embedded.Nullable} and {@link Empty @Embedded.Empty} offer shortcuts for this.
45+
* {@link Nullable @Unwrapped.Nullable} and {@link Empty @Unwrapped.Empty} offer shortcuts for this.
4646
*
4747
* @return never {@link} null.
4848
*/
4949
OnEmpty onEmpty();
5050

5151
/**
52-
* @return prefix for columns in the embedded value object. An empty {@link String} by default.
52+
* @return prefix for columns in the unwrapped value object. An empty {@link String} by default.
5353
*/
5454
String prefix() default "";
5555

5656
/**
57-
* Load strategy to be used {@link Embedded#onEmpty()}.
57+
* Load strategy to be used {@link Unwrapped#onEmpty()}.
5858
*
5959
* @author Christoph Strobl
6060
*/
@@ -63,74 +63,74 @@ enum OnEmpty {
6363
}
6464

6565
/**
66-
* Shortcut for a nullable embedded property.
66+
* Shortcut for a nullable unwrapped property.
6767
*
6868
* <pre class="code">
69-
* &#64;Embedded.Nullable private Address address;
69+
* &#64;Unwrapped.Nullable private Address address;
7070
* </pre>
7171
*
7272
* as alternative to the more verbose
7373
*
7474
* <pre class="code">
75-
* &#64;Embedded(onEmpty = USE_NULL) &#64;javax.annotation.Nonnull(when = When.MAYBE) private Address address;
75+
* &#64;Unwrapped(onEmpty = USE_NULL) &#64;javax.annotation.Nonnull(when = When.MAYBE) private Address address;
7676
* </pre>
7777
*
7878
* @author Christoph Strobl
79-
* @see Embedded#onEmpty()
79+
* @see Unwrapped#onEmpty()
8080
*/
81-
@Embedded(onEmpty = OnEmpty.USE_NULL)
81+
@Unwrapped(onEmpty = OnEmpty.USE_NULL)
8282
@Documented
8383
@Retention(RetentionPolicy.RUNTIME)
8484
@Target({ ElementType.FIELD, ElementType.METHOD })
8585
@javax.annotation.Nonnull(when = When.MAYBE)
8686
@interface Nullable {
8787

8888
/**
89-
* @return prefix for columns in the embedded value object. An empty {@link String} by default.
89+
* @return prefix for columns in the unwrapped value object. An empty {@link String} by default.
9090
*/
91-
@AliasFor(annotation = Embedded.class, attribute = "prefix")
91+
@AliasFor(annotation = Unwrapped.class, attribute = "prefix")
9292
String prefix() default "";
9393

9494
/**
95-
* @return value for columns in the embedded value object. An empty {@link String} by default.
95+
* @return value for columns in the unwrapped value object. An empty {@link String} by default.
9696
*/
97-
@AliasFor(annotation = Embedded.class, attribute = "prefix")
97+
@AliasFor(annotation = Unwrapped.class, attribute = "prefix")
9898
String value() default "";
9999
}
100100

101101
/**
102-
* Shortcut for an empty embedded property.
102+
* Shortcut for an empty unwrapped property.
103103
*
104104
* <pre class="code">
105-
* &#64;Embedded.Empty private Address address;
105+
* &#64;Unwrapped.Empty private Address address;
106106
* </pre>
107107
*
108108
* as alternative to the more verbose
109109
*
110110
* <pre class="code">
111-
* &#64;Embedded(onEmpty = USE_EMPTY) &#64;javax.annotation.Nonnull(when = When.NEVER) private Address address;
111+
* &#64;Unwrapped(onEmpty = USE_EMPTY) &#64;javax.annotation.Nonnull(when = When.NEVER) private Address address;
112112
* </pre>
113113
*
114114
* @author Christoph Strobl
115-
* @see Embedded#onEmpty()
115+
* @see Unwrapped#onEmpty()
116116
*/
117-
@Embedded(onEmpty = OnEmpty.USE_EMPTY)
117+
@Unwrapped(onEmpty = OnEmpty.USE_EMPTY)
118118
@Documented
119119
@Retention(RetentionPolicy.RUNTIME)
120120
@Target({ ElementType.FIELD, ElementType.METHOD })
121121
@javax.annotation.Nonnull(when = When.NEVER)
122122
@interface Empty {
123123

124124
/**
125-
* @return prefix for columns in the embedded value object. An empty {@link String} by default.
125+
* @return prefix for columns in the unwrapped value object. An empty {@link String} by default.
126126
*/
127-
@AliasFor(annotation = Embedded.class, attribute = "prefix")
127+
@AliasFor(annotation = Unwrapped.class, attribute = "prefix")
128128
String prefix() default "";
129129

130130
/**
131-
* @return value for columns in the embedded value object. An empty {@link String} by default.
131+
* @return value for columns in the unwrapped value object. An empty {@link String} by default.
132132
*/
133-
@AliasFor(annotation = Embedded.class, attribute = "prefix")
133+
@AliasFor(annotation = Unwrapped.class, attribute = "prefix")
134134
String value() default "";
135135
}
136136
}

0 commit comments

Comments
 (0)