找到对集合的共享引用:ManyToMany

发布于 2024-12-29 04:54:55 字数 1173 浏览 2 评论 0原文

我有一个实体以多对多的方式连接到它的自身:

我想要有可能嵌套“相关”结果的搜索结果。 相关结果不应出现在多个主要结果下,但它们可以。 相关结果不会有任何相关结果,这只深入一层。

@Entity
@Table(name = "SEARCH_RESULTS")
public class Result {
    @Id
    private Long seq;

    @ManyToMany(fetch = FetchType.LAZY, targetEntity = Result.class)
    @JoinTable(name = "SEARCH_RESULTS", 
       inverseJoinColumns = {
           @JoinColumn(name = "SEQ", referencedColumnName = "SEQ", 
                 updatable = false, insertable = false) },
       joinColumns = { 
           @JoinColumn(name = "related_id", referencedColumnName = "related_id", 
                 updatable = false, insertable = false, nullable = true), })
    @FilterJoinTable(name="SEARCH_RESULTS",condition="IS_PRIMARY_RESULT IS FALSE")
    private List<Result> related;
}

现在,我使用延迟加载和多对多关系,并在获取实体时应用 IS_PRIMARY_RESULT IS TRUE 的条件。

对于简单的嵌套,它工作得很好,但是当连接的“相关”集合在多个主要结果之间共享时,Hibernate 会抱怨。

我得到的例外是:

 org.hibernate.HibernateException: Found shared references to a collection: abc.Results.related

是否有办法使休眠允许共享集合? 我尝试使用 @OneToMany,但是当表有多个具有相同 group_id 的主要结果时,它会出现相同的错误。

仅供参考:这是由存储过程生成的临时表。

I have an entity which is joined to it's self in a ManyToMany fashion:

I want to have search results which may have 'related' results nested.
Related results shouldn't appear under more then one primary result, but they could.
Related results won't have any related results, this only goes one level deep.

@Entity
@Table(name = "SEARCH_RESULTS")
public class Result {
    @Id
    private Long seq;

    @ManyToMany(fetch = FetchType.LAZY, targetEntity = Result.class)
    @JoinTable(name = "SEARCH_RESULTS", 
       inverseJoinColumns = {
           @JoinColumn(name = "SEQ", referencedColumnName = "SEQ", 
                 updatable = false, insertable = false) },
       joinColumns = { 
           @JoinColumn(name = "related_id", referencedColumnName = "related_id", 
                 updatable = false, insertable = false, nullable = true), })
    @FilterJoinTable(name="SEARCH_RESULTS",condition="IS_PRIMARY_RESULT IS FALSE")
    private List<Result> related;
}

Now, I'm using Lazy loading and a ManyToMany relation and applying a Criteria of IS_PRIMARY_RESULT IS TRUE when fetching my entities.

For simple nesting it's working fine, but Hibernate complains when the join'd 'related' collection is shared between multiple primary Results.

The exception I'm getting is:

 org.hibernate.HibernateException: Found shared references to a collection: abc.Results.related

Is there anyway to make hibernate allow the collection to be shared?
I tried using a @OneToMany, but when the table has multiple primary results with the same group_id, it bombs with the same error.

FYI: this is a temp table produced by a stored procedure.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文