hibernate中的mappedBy是根据模式定义定义的还是逻辑定义的?

发布于 2025-01-07 23:49:21 字数 914 浏览 0 评论 0原文

我有以下情况:

一个目的地可以有许多别名

逻辑上:我希望目的地成为这种关系的所有者,因为如果没有目的地,别名就没有任何意义。

但是,数据库架构如下所示:

Destination-Alias 关系

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:

Destination-Alias relationship

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2025-01-14 23:49:21

所有权影响 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文