hibernate中的mappedBy是根据模式定义定义的还是逻辑定义的?
我有以下情况:
一个目的地可以有许多别名
逻辑上:我希望目的地成为这种关系的所有者,因为如果没有目的地,别名就没有任何意义。
但是,数据库架构如下所示:
DestinationAlias 将 idDestination 作为外键,因此 @JoinColumn 中hibernate 注释将位于 DestinationAlias:
Destination:
@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "mainCity")
public Set<DestinationAlias> getAliases() {
return aliases;
}
DestinationAlias:
@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="IDDESTINATION", nullable=false)
public Destination getMainCity() {
return mainCity;
}
根据此定义,DestinationAlias 是此关系的所有者,因为mappedBy
是 Destination 类的一个属性。
Hibernate 是否要求我遵循数据库架构
并将实体标记为关系的所有者,还是可以基于逻辑
原因来完成?
I have the following situation:
One Destination can have Many Aliases
Logically: I would like Destination to be the owner of this relationship, since if there was no destination, alias would not hold any meaning.
However, the database schema is like this:
DestinationAlias has idDestination as the Foreign Key and thus the @JoinColumn in hibernate annotation would be on DestinationAlias:
Destination:
@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "mainCity")
public Set<DestinationAlias> getAliases() {
return aliases;
}
DestinationAlias:
@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="IDDESTINATION", nullable=false)
public Destination getMainCity() {
return mainCity;
}
From this definition, DestinationAlias is the owner of this relationship since the mappedBy
is an attribute on Destination class.
Does hibernate require me to follow the database schema
and mark entity as the owner of the relationship or can it be done based on Logical
reasons ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有权影响 hibernate 对底层数据库表建模的方式。如果省略mappedBy属性,hibernate将在Destination和DestinationAlias之间生成一个连接表(就像MM关系),而不是一个简单的连接列。除此之外,我很好奇你的实际问题是什么。这纯粹是装饰性的,还是存在功能问题?
编辑:因为这似乎是一个纯粹的装饰问题,我的建议是只接受 Hibernate 语义。虽然我同意 Hibernate 将 DestinationAlias 称为“所有者”可能看起来违反直觉,但这只是 Hibernate 决定的命名法。它也不是唯一的,它来自“所有者”是连接列所在位置的概念。
不必要时,不要浪费时间试图强制 hibernate 符合您的定义。
Ownership affects how hibernate would model the underlying database tables. If you omit the mappedBy attribute, hibernate would generate a join-table (like for an M-M relationship) between Destination and DestinationAlias, instead of a simple join column. Other than that, I'm curious as to what your actual issue is here. Is this purely cosmetic, or are there functional issues in play for you here?
EDIT: as this seems to be a purely cosmetic issue, my advice is to just accept the Hibernate semantic. While I agree that it might appear counter-intuitive that Hibernate would call DestinationAlias the "owner", that is just the nomenclature Hibernate decided on. It's not unique either and it comes from the notion that the "owner" is where the join-column resides.
Don't waste time trying to force hibernate to conform to your definitions when you don't have to.