我的Hibernate架构出了问题:
我有一个MappedSuperClass Person,一个Employee和一个Customer。
--> Person.class
@MappedSuperclass
@Audited
public class Person extends PersistentObject {
@Column(name="TITLE")
@Enumerated(EnumType.STRING)
private Title title;
@Column(name="FIRST_NAME")
private String fname = null;
@Column(name="LAST_NAME")
private String lname = null;
--> Employee.class
@Entity
@Table(name="TBL_EMPLOYEE")
@Audited
public class Employee extends Person {
--> Customer.class
@Entity
@Table(name="TBL_CUSTOMER")
@Audited
public class Customer extends Person {
这很有效,但现在我的问题是:我已经扩展了人物'带有项目清单,例如地址/联系信息/等,并对同一类型的所有项目使用单表(所有地址)。
仅将地址添加到客户时,这不是问题:
--> Customer.java
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="customer")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;
--> Address.java
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="CUSTOMER_ID")
private Customer customer;
现在我想将地址扩展到员工。我想,我可以使用Supertype来保存地址,但这似乎不能正常工作:
--> Person.java
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="person")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;
--> Address.java
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PERSON_ID")
private Person person;
运行时,我收到以下错误:@OneToOne or @ManyToOne on at.test.Address.person references an unknown entity: at.test.Person
是否有可能让两个实体对他们的地址使用相同的表? ID不会发生冲突,因为Employee和Customer共享相同的序列。或者我应该以另一种方式设计这个问题?
非常感谢您提前和最好的问候!
答案 0 :(得分:2)
您必须将您的超类声明为@Entity而不是@MappedSuperClass和单表继承策略,因此您可以在ManyToOne关系中引用它
@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="P")
public abstract class Person {
@Id
@GeneratedValue
@Column(name = "PERSON_ID")
private Long personId;
private String firstname;
private String lastname;
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="customer")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;
}
@Entity
@Table(name="PERSON")
@DiscriminatorValue("E")
public class Employee extends Person {
}
@Entity
@Table(name="PERSON")
@DiscriminatorValue("C")
public class Customer extends Person {
}
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PERSON_ID")
private Person person;
}
答案 1 :(得分:0)
从Flo的链接我意识到用于我的问题的正确范例是“每个子类一个表”一个。谢谢你的帮助!
答案 2 :(得分:0)
在Address类中,您必须将参数insertable和updatable设置为false。它实际上是有道理的,因为如果你不这样做。 JPA允许您创建一些“Person”(可能未映射)对象,将其分配给地址,然后与地址对象一起保留。尝试使用以下代码:
@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING)
public abstract class Person {
@Id
@GeneratedValue
@Column(name = "PERSON_ID")
private Long personId;
private String firstname;
private String lastname;
@OneToMany(cascade = {CascadeType.ALL}, mappedBy="customer")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Address> addresses = null;
}
@Entity
@Table(name="PERSON")
@DiscriminatorValue("E")
public class Employee extends Person {
//some fields
}
@Entity
@Table(name="PERSON")
@DiscriminatorValue("C")
public class Customer extends Person {
//some fields
}
@Entity
@Table(name="TBL_ADDRESSES")
@Audited
public class Address extends PersistentObject {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PERSON_ID", insertable = false, updatable = false)
private Person person;
}