[英]How to annotate composition of abstract class instance?
I have a design of Person
as shown in the following picture. 我有一个
Person
设计,如下图所示。 The design is to allow a person to change his role from Staff
to Student
, Student
to Faculty
and so on. 该设计是为了让一个人从改变自己角色
Staff
向Student
, Student
对Faculty
等等。
I want to persist this system to DB using hibernate annotation. 我想使用休眠注释将该系统持久化到数据库。 Does anyone know how to do that?
有谁知道这是怎么做到的吗?
Thanks a lot! 非常感谢!
So you have 1:N relation between Entity Persona and abstract entity Person Role. 因此,您在实体角色和抽象实体角色之间具有1:N的关系。 Think this may work for you.
认为这可能对您有用。
@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 {
...
}
Also don't forget to set proper 也不要忘记设置适当的
@Inheritance(strategy=InheritanceType.<strategy>)
to define how class model is mapped to relational model. 定义如何将类模型映射到关系模型。
Edit: Unfortunately @MappedSuperclass can't be used in relations mapping so this is not an option here as long as you would like to have PersonRole collection in Person entity. 编辑:不幸的是,@ MappedSuperclass不能用于关系映射,因此,只要您希望在Person实体中拥有PersonRole集合,就不能在这里使用此选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.