使用 id 而不是对象创建 JPA 多对多关系

发布于 2024-11-07 07:02:43 字数 2423 浏览 0 评论 0原文

我有一个大型数据集,我正在批量导入该数据集,其中的行看起来像

(news_id, category_id_1, category_id_2, ..., category_id_9)

每个 category_id_x 都是来自一组固定类别的整数。

我想将这些多个类别映射到 m2m 关系中,以便更快地搜索。

我有一个 News 表和一个 Category 表。

Category 表将类别 ID 映射到类别名称。

我已经在 J​​PA 中为各种 News 字段设置了所有映射,并且我想重用此代码。

我的问题是如何在 JPA 中导入这些 m2m 关系。

我正在考虑以下内容,但收到错误消息,指出您无法手动创建类别对象。

// News object
@Entity
@Table(name = "news", schema = "public", uniqueConstraints = {})
public class News implements java.io.Serializable {

    // Fields
    @Column(name = "asx_code")
    private String asxCode;
    @Id
    @Column(name = "annnum", unique = true, nullable = false)
    private String annnum;
    @Column(name = "company_name")
    private String companyName;
    ...
    ...
    @ManyToMany() 
    @JoinTable(name="announcement_types", 
        joinColumns= @JoinColumn(name="annnum"),
        inverseJoinColumns=@JoinColumn(name="report_type"))
    private Collection<ReportType> reportType;

function m(String) {
    // extract and return data from datasource
}

// Create a news object from 9 different report types
News o = new News();        
java.util.Collection<ReportType> types = new HashSet(); 
types.add(new ReportType(toInt(m("RepType0"))));
types.add(new ReportType(toInt(m("RepType1"))));
types.add(new ReportType(toInt(m("RepType2"))));
types.add(new ReportType(toInt(m("RepType3"))));
types.add(new ReportType(toInt(m("RepType4"))));
types.add(new ReportType(toInt(m("RepType5"))));
types.add(new ReportType(toInt(m("RepType6"))));
types.add(new ReportType(toInt(m("RepType7"))));
types.add(new ReportType(toInt(m("RepType8"))));
types.add(new ReportType(toInt(m("RepType9"))));
o.setReportType(types);

// Query
try {
    EntityTransaction entr = em.getTransaction();
    entr.begin();           
    em.persist(row); # row is our News object
    entr.commit();
} catch (Exception e) {
    e.printStackTrace();
    return false;
}
return true;

// ERROR
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: edu.unsw.eventstudy.shared.dto.ReportType

编辑:

似乎我正在处理独立实体的概念。在 Hibernate 中,可以使用未保存值映射或 isSaved 属性解决此问题。现在在JPA中寻找解决方案。

I have a large dataset which I am importing in bulk with rows looking like

(news_id, category_id_1, category_id_2, ..., category_id_9)

Each category_id_x is an integer from a fixed set of categories.

I want to map these multiple categories into a m2m relation for quicker searching.

I have a News table and a Category table.

The Category table maps category ids to category names.

I already have all the mappings setup in JPA for the various News fields, and I want to reuse this code.

My question is how can I import these m2m relations in JPA.

I was thinking along the lines of the following but I get errors saying that you can't create Category objects manually.

// News object
@Entity
@Table(name = "news", schema = "public", uniqueConstraints = {})
public class News implements java.io.Serializable {

    // Fields
    @Column(name = "asx_code")
    private String asxCode;
    @Id
    @Column(name = "annnum", unique = true, nullable = false)
    private String annnum;
    @Column(name = "company_name")
    private String companyName;
    ...
    ...
    @ManyToMany() 
    @JoinTable(name="announcement_types", 
        joinColumns= @JoinColumn(name="annnum"),
        inverseJoinColumns=@JoinColumn(name="report_type"))
    private Collection<ReportType> reportType;

function m(String) {
    // extract and return data from datasource
}

// Create a news object from 9 different report types
News o = new News();        
java.util.Collection<ReportType> types = new HashSet(); 
types.add(new ReportType(toInt(m("RepType0"))));
types.add(new ReportType(toInt(m("RepType1"))));
types.add(new ReportType(toInt(m("RepType2"))));
types.add(new ReportType(toInt(m("RepType3"))));
types.add(new ReportType(toInt(m("RepType4"))));
types.add(new ReportType(toInt(m("RepType5"))));
types.add(new ReportType(toInt(m("RepType6"))));
types.add(new ReportType(toInt(m("RepType7"))));
types.add(new ReportType(toInt(m("RepType8"))));
types.add(new ReportType(toInt(m("RepType9"))));
o.setReportType(types);

// Query
try {
    EntityTransaction entr = em.getTransaction();
    entr.begin();           
    em.persist(row); # row is our News object
    entr.commit();
} catch (Exception e) {
    e.printStackTrace();
    return false;
}
return true;

// ERROR
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: edu.unsw.eventstudy.shared.dto.ReportType

EDIT:

Seems I am dealing with the concept of detached entities. This is solved in Hibernate using the unsaved-value mapping or isSaved property. Now looking for a solution in JPA.

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

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

发布评论

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

评论(1

爱,才寂寞 2024-11-14 07:02:43

我只看到 1 个方向的关联。也许您忘记执行另一项操作,例如:
reportType0.add(新闻);
reportType1.add(新闻);
...
reportTypeN.add(新闻);

此外,您正在“创建”ReportType 的实例,而不是从数据库中收集它们。它们是否已存在于数据库中?

I only see the association in 1 direction. Maybe you forgot to do the other one, like:
reportType0.add(news);
reportType1.add(news);
...
reportTypeN.add(news);

Also, you are "creating" instances of the ReportType instead of gathering them from the database. Do they already exist in the database?

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