2015-03-25 104 views
0

我有一个Person的设计,如下图所示。设计是让一个人把他的角色从Staff改为Student,Student改为Faculty等等。如何注释抽象类实例的组合?

我想使用hibernate注释将此系统持久化到数据库。有谁知道这是怎么做到的吗?

非常感谢!

enter image description here

回答

0

所以,你有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集合。