[hibernate-dev] [OGM] Mapping of embedded

Guillaume Smet guillaume.smet at gmail.com
Wed Jul 13 09:22:51 EDT 2016


So this is a followup of my previous email (looks like I found out the
Send shortcut on GMail...)... which was supposed to be a followup of:
 * https://hibernate.atlassian.net/browse/OGM-893
 * https://github.com/hibernate/hibernate-ogm/pull/728
 * and a discussion we had yesterday with Davide.

Taking MongoDB as the reference implementation here.

Current situation
=================

1/ for a simple embedded Address (property: homeAddress) with one of
the property having a @Column(name = "postal_code"), we end up with
the following mapping:
{
    [...],
    'homeAddress' : {
        'city' : 'Paris',
        'country' : 'France',
        'street1' : '1 avenue des Champs Elysees',
        'type' : {
            'name' : 'main'
        }
    },
    'postal_code' : '75007',
    [...]
}
As you can see, postal_code is stored outside of the nested structure.
This is in line with the ORM behavior where you end up having the
columns homeAddress_city and postal_code.

Of course, this is a bit weird when we are considering nested documents.

See EmbeddableMappingTest for reference.

2/ now suppose that we have a List<Address>, the current mapping is
the following:
{
    [...],
    "addresses": [
       {
           "addresses": {
               "country": "Germany",
               "city": "Paris",
               "street1": "1 avenue des Champs Elysees"
           },
            "postal_code": "75007"
       },
       {
           "addresses": {
               "city": "Rome",
               "street1": "Piazza del Colosseo, 1",
               "type": {
                   "name": "primary"
               }
           },
            "postal_code": "00184"
       }
    ],
    [...]
}

Note the fact that addresses is nested twice.

This is discussed at length in https://hibernate.atlassian.net/browse/OGM-893.

What does PR 728 change?
========================

After the work we did with Steve on ORM and the followup of Davide on
OGM, we end up with the following situation:

1/ Same as before. postal_code is outside of the nested document.

2/ This is where the behavior has changed, we now have the following mapping:
{
    "addresses": [
       {
           "country": "Germany",
           "city": "Paris",
           "street1": "1 avenue des Champs Elysees",
           "postal_code": "75007"
       },
       {
           "city": "Rome",
           "street1": "Piazza del Colosseo, 1",
           "type": {
               "name": "primary"
           },
           "postal_code": "00184"
       }
    ],
}

Note that postal_code and city are now at the same level.

See this future test executed on top of pr/728:
https://gist.github.com/gsmet/0652294523b2c48efe72668ccc0a6e1c

Conclusion
==========

So as you can see, the mapping is quite different between a simple
embedded and a list of embedded. I was not very happy with the
behavior of 2/ before, especially because if you remove the @Column,
you lose the data stored. Same if you didn't have one before and you
add one to your embeddable.

But I'm also not convinced having a different behavior between 1/ and
2/ is the way to go. Note that, from what I've seen, I don't think
changing 1/ to move postal_code in the nested document will be easy
(or even feasible).

Opinions? Thoughts?

-- 
Guillaume


More information about the hibernate-dev mailing list