Skip to content

DATAMONGO-1902 - Add support for embedding objects. #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

christophstrobl
Copy link
Member

@christophstrobl christophstrobl commented Dec 15, 2020

Depends on: spring-projects/spring-data-commons#2293

⚠️ Open for discussion - do not merge!


Embedded Types

Embedded entities are used to design value objects in your Java domain model whose properties are flattened out into the MongoDB Document.

Embedded Types Mapping

In the example below you see, that User.name is annotated with @Embedded.
The consequence of this is that all properties of UserName are folded into the user document.

public class User {
    @Id
    private String userId;

    @Embedded(onEmpty = USE_NULL)
    private UserName name;
}

public class UserName {
    private String firstname;
    private String lastname;
}
{
  "_id" : "1da2ba06-3ba7",
  "firstname" : "Emma",
  "lastname" : "Frost"
}

It is possible to use complex types within an embedded object.
However those must not be, nor contain embedded fields themselves.

@mp911de
Copy link
Member

mp911de commented Jan 26, 2021

In case we don't feel comfortable merging the related PR in Spring Data Commons (spring-projects/spring-data-commons#480), let's move forward by introducing a store-specific embedded-annotation and sort out the duplications in PersistentEntity/PersistentProperty at a later stage.

@mp911de mp911de self-assigned this Jan 29, 2021
christophstrobl and others added 2 commits February 16, 2021 11:33
We now support embedded types in the sense of unwrapping nested objects into their parent Document to flatten out domain models where needed.

A domain class of:

public class User {
    @id
    private String userId;

    @Embedded(onEmpty = USE_NULL)
    private UserName name;
}

public class UserName {
    private String firstname;
    private String lastname;
}

renders:

{
  "_id" : "1da2ba06-3ba7",
  "firstname" : "Emma",
  "lastname" : "Frost"
}
Reorder API methods, remove unused MongoPersistentProperty.isNullable method, reduce visibility where possible. Add Javadoc and tweak documentation wording.

Introduce DotPath utility to abstract dot path concatenation.
@mp911de mp911de force-pushed the issue/DATAMONGO-1902 branch from c89d2e3 to 3e00835 Compare February 16, 2021 10:34
@mp911de mp911de marked this pull request as ready for review February 16, 2021 13:45
mp911de pushed a commit that referenced this pull request Feb 16, 2021
We now support embedded types in the sense of unwrapping nested objects into their parent Document to flatten out domain models where needed.

A domain class of:

public class User {
    @id
    private String userId;

    @Embedded(onEmpty = USE_NULL)
    private UserName name;
}

public class UserName {
    private String firstname;
    private String lastname;
}

renders:

{
  "_id" : "1da2ba06-3ba7",
  "firstname" : "Emma",
  "lastname" : "Frost"
}

Resolves #2803.
Original pull request: #896.
mp911de added a commit that referenced this pull request Feb 16, 2021
Reorder API methods, remove unused MongoPersistentProperty.isNullable method, reduce visibility where possible. Add Javadoc and tweak documentation wording.

Introduce DotPath utility to abstract dot path concatenation.

See #2803.
Original pull request: #896.
@mp911de mp911de added this to the 3.2 M3 (2021.0.0) milestone Feb 16, 2021
@mp911de
Copy link
Member

mp911de commented Feb 16, 2021

That's merged and polished now.

@mp911de mp911de closed this Feb 16, 2021
@mp911de mp911de deleted the issue/DATAMONGO-1902 branch February 16, 2021 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support flattening embedded/nested objects [DATAMONGO-1902]
2 participants