Search code examples
mongodbhibernatehibernate-mappinghibernate-ogm

Hibernate OGM mapping to subcollection


I have an collection as following

application
 * _id
 * name
 * desc
 * settings
 ** _id
 ** magento
 *** name
 *** keys

I use the following object to map the document

@Entity
@Table(name = "applications")
public class ApplicationEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
private String id;

@Column(name = "applicationName")
private String name;

@Column(name = "desc")
private String desc;

@Embedded
@Column(name = "settings.magento")
private MagentoSettings magentoSettings;

However, the object "MangetoSettings" could not be mapped and it return null.

My question is that how I can map sub document (magento) without declare the parent (settings) in my object?

Let's say the "Settings" document contains only "Magento" and it would be wasted if declare "Settings" object with single property.

Thanks


Solution

  • I found the answer in the Jboss Hibernate doc here

    You can override the column name used for a property of an embedded object. But you need to know that the default column name is the concatenation of the embedding property, a . (dot) and the embedded property (recursively for several levels of embedded objects).

    AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="settings.magento.name")),
        @AttributeOverride(name="key", column=@Column(name="settings.magento.key"))
        })
    private MagentoSettings magentoSettings;
    

    Thanks