我想在“地址”实体中保存“用户”和“雇员”实体的地址。 如何设计我的地址实体,以便能够保存用户和雇员的地址。
答案 0 :(得分:3)
在“用户”和“员工”子类中扩展“人”类。
@MappedSuperclass abstract class Person { @ManyToOne @JoinColumn(name="ADDRESS") protected Address address; } @Entity class Employee extends Person {} @Entity class User extends Person {}
答案 1 :(得分:1)
您有两种选择,一种是使地址实体可嵌入,
@Embeddable
public class Address {
....
}
@Entity
public class User {
.....
@Embedded
private Address address;
}
@Entity
public class Employee {
.....
@Embedded
private Address address;
}
这将复制表Employee和表User中的列。
如果要共享地址,另一种方法是将地址用作表格:
@Entity
public class Address {
....
}
@Entity
public class User {
.....
@ManyToOne
private Address address;
}
@Entity
public class Employee {
.....
@ManyToOne
private Address address;
}