gpt4 book ai didi

java - x.person 上的@OneToOne 或@ManyToOne 引用未知实体 : y. Person - 继承问题

转载 作者:行者123 更新时间:2023-11-30 09:18:59 25 4
gpt4 key购买 nike

我的 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;

现在我还想将地址扩展到 Employee。我想,我可以使用 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;

运行时,出现以下错误:at.test.Address.person 上的@OneToOne 或@ManyToOne 引用了一个未知实体:at.test.Person

是否有可能让两个实体使用相同的地址表? ID 不会冲突,因为 Employee 和 Customer 共享相同的序列。或者我应该换一种方式设计这个问题?

非常感谢您,并致以最诚挚的问候!

最佳答案

您必须将您的父类(super class)声明为@Entity 而不是@MappedSuperClass 和单表继承策略,以便您可以在多对一关系中引用它

@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;
}

关于java - x.person 上的@OneToOne 或@ManyToOne 引用未知实体 : y. Person - 继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com