【问题标题】:Mapping a polymorphic list with JPA?使用 JPA 映射多态列表?
【发布时间】:2013-03-11 05:00:22
【问题描述】:

假设我有:

//inheritance annotations 
public abstract class Animal {
    private int noOfLegs;
    //...
}

//inheritance annotations 
public class Cat extends Animal {
    //cat stuff
}

//inheritance annotations 
public class Dog extends Animal {
    //dog stuff
}

@Entity
public class House {
    @OneToMany
    @JoinTable(//join stuff
    private List<Animal> animals;
    //hose stuff
}

现在,如果我将猫和狗都放入我的房子,并坚持我的房子,JPA 是否知道将狗和猫放入相应的表中(或使用任何继承映射策略)。也知道查询的时候怎么取回来吗?

【问题讨论】:

    标签: java hibernate inheritance jpa orm


    【解决方案1】:

    简短的回答是肯定的。但是对于继承映射的原生 hibernate 支持比标准 JPA 更强大,并且“每个类层次结构一个表”或“每个子类一个表”映射策略比“每个具体类一个表”策略更适合多态查询和关联。

    另见:

    【讨论】:

    • 即使指向文档的链接可能对解决问题有用,也可能会受到赞赏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2010-10-22
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多