【问题标题】:How to annotate composition of abstract class instance?如何注释抽象类实例的组合?
【发布时间】:2015-05-28 13:50:58
【问题描述】:

我有一个Person 的设计,如下图所示。设计是允许一个人将他的角色从Staff更改为StudentStudent更改为Faculty等等。

我想使用休眠注释将此系统持久化到数据库。有人知道怎么做吗?

非常感谢!

【问题讨论】:

    标签: java hibernate jpa persistence hibernate-mapping


    【解决方案1】:

    因此,Entity Persona 和抽象实体 Person Role 之间存在 1:N 关系。认为这可能对你有用。

    @Entity
    public class Person {
      // Here you have all roles in some collection.
      @OneToMany(mappedBy="person", fetch=FetchType.LAZY)
      private List<PersonRole> roles;
      ...
    }
    
    @Entity
    public abstract class PersonRole {
        @ManyToOne
        @JoinColumn(name="PERSON_ID")
        private Person person;
        ...
    }
    
    @Entity
    public class Staff extends PersonRole {
       ...
    }
    

    也别忘了设置正确

    @Inheritance(strategy=InheritanceType.<strategy>)
    

    定义类模型如何映射到关系模型。

    编辑:不幸的是,@MappedSuperclass 不能用于关系映射,所以只要您希望在 Person 实体中拥有 PersonRole 集合,这不是一个选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 2021-10-16
      • 2014-02-19
      • 2012-02-28
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多