找到对集合的共享引用:ManyToMany
我有一个实体以多对多的方式连接到它的自身:
我想要有可能嵌套“相关”结果的搜索结果。 相关结果不应出现在多个主要结果下,但它们可以。 相关结果不会有任何相关结果,这只深入一层。
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论