【问题标题】:Found shared references to a collection: OneToMany empty collections refering to same PersistentBag找到对集合的共享引用:OneToMany 空集合引用相同的 PersistentBag
【发布时间】:2020-02-27 10:43:40
【问题描述】:

我有以下模型:一个Organization 有一个List<Contract>,每个Contract 可能有一个pricebookId 和一个PricebookEntry 的集合。多个Contract 实体可以具有相同的pricebookIdCollection<PricebookEntry>@OneToManypricebookId 关联为 JoinColumn 的结果。

这是假设:一个组织有 2 个合同,两个合同都没有pricebookId

这是我的问题:在涉及此特定组织的 Hibernate 会话期间,Hibernate 抛出此异常: org.hibernate.HibernateException: Found shared references to a collection: Contract.pricebookEntries.

事实上,两个合约的pricebookEntries 都设置为相同的PersistentBag(相同的引用)。

我该如何解决这个问题?

感谢您的帮助!

@Entity
public class Organization implements Serializable {

  @Id
  private String id;

  @Fetch(FetchMode.SUBSELECT)
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
  private final List<Contract> contracts = new ArrayList<>();

}


@Entity
public class Contract implements Serializable {

  @Id
  private String id;

  @Column(name = "pricebook_id")
  private String pricebookId;

  @JoinColumn(name = "organization_id", referencedColumnName = "id")
  @ManyToOne(fetch = FetchType.LAZY)
  private Organization organization;

  @JoinColumn(name="pricebookId", referencedColumnName = "pricebook_id")
  @OneToMany(fetch = FetchType.EAGER)
  @Fetch(FetchMode.SUBSELECT)
  private final Collection<PricebookEntry> pricebookEntries = new ArrayList<>();

}

@Entity
public class PricebookEntry {

  @Id
  private String id;

  private String pricebookId;

}

【问题讨论】:

    标签: java spring hibernate jpa spring-data-jpa


    【解决方案1】:

    您在这里所拥有的不是@OneToMany 关系,因为多个Contract 实体可能引用相同(集合)的PricebookEntry 实例。

    修复将Collection&lt;PricebookEntry&gt; 转换为适当的实体Pricebook 并具有从ContractPricebook 的多对一关系。 也失去了现在多余的pricebookid,因为它只是Pricebook的id。

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 2010-12-14
      • 1970-01-01
      • 2011-08-07
      • 2012-10-14
      • 2020-07-19
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多