场景如下(表格所示)
Delivery table
------
id channelId type
10 100 fax
20 200 email
Fax table
----
id number
100 1234567
101 1234598
Email table
-----
id email
200 a@a.com
201 b@b.com
交付和 channel 实体之间基本上是一对一的关系,但由于每个具体 channel (传真、电子邮件)都有不同的成员,我想在两个实体之间创建一个通用接口(interface)( channel )并将其用于@OneToOne关系。在我看来,这是一个简单的场景,你们中的很多人可能已经经历过,但我无法成功。我试着放那个 targetEntity 东西但没用。仍然说“交付引用了一个未知实体”
有什么想法吗?提前致谢
最佳答案
如何为 Channel
和 TABLE_PER_CLASS
继承策略使用 abstract
父类(super class)?像这样:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Channel {
@Id
@GeneratedValue
private Long id;
// ...
}
@Entity
public class Fax extends Channel {
}
@Entity
public class Email extends Channel {
}
@Entity
public class Delivery {
@Id
@GeneratedValue
private Long id;
@OneToOne
private Channel channel;
// ...
}
关于java - 两个相关 JPA 实体之间的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2748039/