两个相关 JPA 实体之间的接口

发布于 2024-08-30 22:55:19 字数 653 浏览 5 评论 0原文

该场景如下(所示表格)

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] 

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

有什么想法吗?提前致谢

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] 

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"

Any ideas? thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-09-06 22:55:19

Channel 使用 abstract 超类和 TABLE_PER_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;

    // ...
}

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;

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文