【问题标题】:Create a JPA many-to-many relation using an id instead of an object使用 id 而不是对象创建 JPA 多对多关系
【发布时间】:2011-05-13 19:03:17
【问题描述】:

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

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

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

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

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

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

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

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

我正在按照以下思路进行思考,但我收到错误消息,提示您无法手动创建 Category 对象。

// 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 中寻找解决方案。

【问题讨论】:

  • 请复制完整的错误信息,以及触发它的代码。
  • 添加了上面的代码。基本上对于每个新闻项目,我想在第二个表中为其所有报告类型创建条目,但是条目已经存在于第二个表中,并且我已经知道要关联的键。

标签: hibernate jpa many-to-many


【解决方案1】:

我只看到 1 个方向的关联。也许你忘了做另一个,比如: reportType0.add(news); reportType1.add(news); ... reportTypeN.add(news);

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

【讨论】:

  • 是的,它们已经存在于数据库中。我想创建 News 对象,而不必从数据库中查找 ReportType 映射,因为我的导入数据集中已经有了主键。
  • 我不是 100% 确定,但我认为您确实需要从数据库中收集 ReportType 实例,并更新它们的 News 集合。
  • 这是我想要避免的,因为从数据库中获取 ReportTypes 似乎毫无意义,因为我已经有了它们的主键。
  • 截至今天,您找到比我更好的解决方案了吗?
猜你喜欢
  • 2014-03-07
  • 2014-04-14
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2017-06-23
相关资源
最近更新 更多