gpt4 book ai didi

jpa - 如何在 JPA 中拥有 2 个相同类型的集合? (EclipseLink)

转载 作者:行者123 更新时间:2023-12-04 06:32:10 25 4
gpt4 key购买 nike

想知道如何做 How to have 2 collections of the same type in JPA? 中指定的事情,但使用 EclipseLink 而不是 Hibernate。请参阅下面的帖子:

I've got 2 entities in JPA: Entry and Comment. Entry contains two collections of Comment objects.


@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@IndexColumn(base = 1, name = "dnr")
private List<Comment> descriptionComments = new ArrayList<Comment>();

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@IndexColumn(base = 1, name = "pmnr")
private List<Comment> postMortemComments = new ArrayList<Comment>();

To store such objects, JPA+Hibernate creates "Entry" table, "Comment" table and SINGLE "Entry_Comment":

create table Entry_Comment (Entry_id integer not null, postMortemComments_id integer not null, pmnr integer not null, descriptionComments_id integer not null, dnr integer not null, primary key (Entry_id, dnr), unique (descriptionComments_id), unique (postMortemComments_id))

Storing of objects fail as descriptionComments_id and postMortemComments_id cannot be "not null" at the same time.

How do I store object containing two collections of the same type using JPA+Hibernate?



我确信将该解决方案转换为 EclipseLink 非常简单,但我似乎无法弄清楚。谢谢

最佳答案

您可以通过 @JoinTable 显式指定连接表的名称。

关于jpa - 如何在 JPA 中拥有 2 个相同类型的集合? (EclipseLink),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5268315/

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