本文介绍了两个相关JPA实体之间的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景如下(显示的表格)

The scenario is as below (tables shown)

Delivery table
------
id  channelId   type
10  100         fax
20  200         email

Fax table
----
id   number
100  1234567
101  1234598

Email table
-----
id   email
200  [email protected]
201  [email protected]

基本上是交付和渠道实体之间的一对一关系,但由于每个具体渠道(传真, email)有不同的成员我想在两个实体之间创建一个通用接口(通道)并将其用于@OneToOne关系。在我看来这是一个简单的场景,你们很多人可能已经经历过,但我无法成功。我试过把那个targetEntity的东西但没有用。仍然说交付引用未知实体

basically a one to one relationship between the delivery and the channel entity but since each concrete channel(fax, email) has different members I want to create a generic interface (channel) between the two entities and use it for the @OneToOne relationship. Seems to me a simple scenario where lot of you might have already gone through but I'm unable to succeed. I tried putting that targetEntity thing but no use. Still says "delivery references an unknown entity"

任何想法?提前谢谢

推荐答案

如何使用 abstract 超类来获取频道 TABLE_PER_CLASS 继承策略?这样的事情:

What about using an abstract super class for the Channel and a TABLE_PER_CLASS inheritance strategy? Something like this:

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

    // ...
}

这篇关于两个相关JPA实体之间的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:48