具有多个自关系(递归关系)的 Hibernate 实体
Hibernate Entity with more than one self relations (recursive relations)
我有一个名为 Article
的 Hibernate 实体,从数据库的角度来看,我想将下一章 ID 和上一章 ID 存储在同一个 table 中,因此有两个一对一的table 与自身以及 nextArticleId
和 previousArticleId
的一种关系是来自同一个 table 的外键。
我的问题是,在为此创建 Hibernate 实体时,是否有建议的方法来处理这种情况?或者我只是将其视为两个正常的一对一关系,并且在 Article
实体中只有两个不同的 Article
对象来表示下一篇文章和上一篇文章?
此致,
问题:
Is that is there a proposed way to deal with situation when creating a Hibernate entity for this?
回答:
Treat this as two normal one-to-one relations and just have two different Article objects within the Article
有这样的映射是正常的方式
class Article {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_article_prev")
private Article previouse;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_article_next")
private Article next;
}
我有一个名为 Article
的 Hibernate 实体,从数据库的角度来看,我想将下一章 ID 和上一章 ID 存储在同一个 table 中,因此有两个一对一的table 与自身以及 nextArticleId
和 previousArticleId
的一种关系是来自同一个 table 的外键。
我的问题是,在为此创建 Hibernate 实体时,是否有建议的方法来处理这种情况?或者我只是将其视为两个正常的一对一关系,并且在 Article
实体中只有两个不同的 Article
对象来表示下一篇文章和上一篇文章?
此致,
问题:
Is that is there a proposed way to deal with situation when creating a Hibernate entity for this?
回答:
Treat this as two normal one-to-one relations and just have two different Article objects within the Article
有这样的映射是正常的方式
class Article {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_article_prev")
private Article previouse;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_article_next")
private Article next;
}