0

I'm using mongodb to store json documents, and since I'm using Hibernate ORM for my relational models I've decided to use the OGM for the mongo ones.

Currently all of my OGM entities share the same parent class, it looks something like:

@Entity
public abstract class Document {
    private static final Gson GSON = new Gson();

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

    public String id() {
        return this.id;
    }

    @Override
    public String toString() {
        return Document.GSON.toJson(this);
    }
}

@Entity
public class Address extends Document {
    private String city;
    private String street;
    private int house;
}

@Entity
public class Person extends Document {
    private String name;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Address> addresses;
}

(simplified of course)

What I expected that would happen when I persist a Person instance is that two collections will be created in the db, one for Person and the other for Address, which I inferred:

The various inheritance strategies are not supported by Hibernate OGM, only the table per concrete class strategy is used

(Supported entity mapping - Hibernate OGM documentation)

But what happens in reality is that only one collection is created with the name Document with two documents in it:

{ 
    _id : id1, 
    DTYPE : Person, 
    name : name of person
}

{ 
    _id : id2, 
    DTYPE : Address, 
    city : City of address,
    street : Street of address
    house : 3
}

What am I missing?
Thanks

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299

1 Answers1

1

I think, it should be:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Document {
...
}
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Yeah, wish it would have been that easy, but it's not.. Following the part I quoted in my question (from the docs) it states that: `Simply do not use @Inheritance nor @DiscriminatorColumn` – Nitzan Tomer Jan 26 '16 at 09:50
  • 1
    My initial reply was based on the documentation only without actually testing it, but then I've decided that it wouldn't hurt to give it a try, and guess what, it does work, even though the documentation says not to. Thanks – Nitzan Tomer Jan 26 '16 at 23:12
  • 1
    Cool, I will open a JIRa to update the documentation – Davide D'Alto Jan 27 '16 at 11:50