【问题标题】:Table per concrete class with Hibernate OGM and mongodb每个带有 Hibernate OGM 和 mongodb 的具体类的表
【发布时间】:2016-01-26 17:09:51
【问题描述】:

我正在使用 mongodb 来存储 json 文档,并且由于我将 Hibernate ORM 用于我的关系模型,因此我决定将 OGM 用于 mongo 的。

目前我所有的 OGM 实体共享同一个父类,它看起来像:

@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;
}

(当然是简化的)

当我坚持 Person 实例时,我预计会在数据库中创建两个集合,一个用于Person,另一个用于Address,我推断:

Hibernate OGM 不支持各种继承策略, 仅使用每个具体类策略的表

(Supported entity mapping - Hibernate OGM documentation)

但实际情况是只创建了一个名为 Document 的集合,其中包含两个文档:

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

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

我错过了什么?
谢谢

【问题讨论】:

    标签: mongodb hibernate hibernate-ogm


    【解决方案1】:

    我想,应该是:

    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public abstract class Document {
    ...
    }
    

    【讨论】:

    • 是的,希望它会那么容易,但事实并非如此。在我在我的问题(来自文档)中引用的部分之后,它指出:Simply do not use @Inheritance nor @DiscriminatorColumn
    • 我最初的回复仅基于文档而没有实际测试,但后来我决定试一试不会有什么坏处,猜猜看,它确实有效,即使文件说不要。谢谢
    • 酷,我会打开一个JIRa来更新文档
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2015-11-11
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多