如何对 @Any 带注释的属性进行双向映射?

发布于 2024-09-08 23:33:12 字数 381 浏览 4 评论 0原文

在本文中 http://www.jroller.com/eyallupu/entry/hibernate_the_any_annotation也在这个问题中 如何使用 Hibernate @Any 相关注释?,解释了如何使用@Any注解。但我如何才能借阅每张 DVD/VHS/书籍呢?如何在 DVD/VHS/BOOK 上进行映射定义?

In this article http://www.jroller.com/eyallupu/entry/hibernate_the_any_annotation and also in this question How to use Hibernate @Any-related annotations?, how @Any annotation can be used was explained. But how can I get borrows for each DVD/VHS/BOOK? How can I do mapping definition on DVD/VHS/BOOK?

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

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

发布评论

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

评论(2

一个人的旅程 2024-09-15 23:33:12

我不认为这是受支持的,并且如文档中所述:

2.4.5.2。 @Any

@Any 注释定义了一个
类的多态关联
来自多个表。这类
映射总是需要多个
柱子。第一列包含
关联实体的类型。这
其余列保存标识符。
无法指定外国
此类的关键约束
关联,所以这肯定是
并不意味着通常的映射方式
(多态)关联。你应该
仅在非常特殊的情况下使用它
(例如审核日志、用户会话数据、
等)。

虽然我知道引入此注释是为了将关联属性映射到没有共同祖先实体的不同类型的实体,但我认为最好为其他实体引入基本类型将继承自双向关系。

另请参阅

I don't think this is supported and, as mentioned in the documentation:

2.4.5.2. @Any

The @Any annotation defines a
polymorphic association to classes
from multiple tables. This type of
mapping always requires more than one
column. The first column holds the
type of the associated entity. The
remaining columns hold the identifier.
It is impossible to specify a foreign
key constraint for this kind of
association, so this is most certainly
not meant as the usual way of mapping
(polymorphic) associations. You should
use this only in very special cases
(eg. audit logs, user session data,
etc).

While I understand that this annotation has been introduced to map an association property to different types of entities that don't have a common ancestor entity, I think it would be better to introduce a base type the other entities would inherit from for bidirectional relations.

See also

雪花飘飘的天空 2024-09-15 23:33:12

是的,一侧是多态的(用 @Any 或 @ManyToAny 映射)的双向关联的问题是 Hibernate 自动生成无效的外键。就我个人而言,我认为这是一个错误,而不是使用错误。

您可以通过显式指定外键来解决这个问题,即不依赖 Hibernate 来推断它。在借用<--> DVD/VHS/Book 示例,假设您希望在“借用”和 DVD/VHS/Book(“项目”)之间建立双向、多对一的关联,然后将其映射到“借用” 一侧使用多态@Any 机制来item,并且在item 一侧使用@OneToMany 来借用。

但是:在后一个属性/获取器上,您还明确指定要使用的连接列,例如“ITEM_ID”。这应该强制Hibernate仅使用“ITEM_ID”,并且(正如我所见)ITEM_ID + ITEM_TYPE,它默认从另一个上的@Any定义推断出来边。

如果您没有 DVD/VHS/Book 的“Item”超类,则必须在每个类中声明它,例如:

@Entity
class Book {
    ...
    @OneToMany
    @JoinColumn(name="item_id")
    public List<Borrow> getBorrows() {
        return this.borrows;
    }
}

Yes, the problem with bi-directional associations where one side is polymorphic (mapped with @Any or @ManyToAny), is that Hibernate auto-generates an invalid foreign key. Personally, I view this as a bug, not a usage error.

You can get around this by specifying the foreign key explicitly, i.e. not rely on Hibernate to infer it. In the Borrow <--> DVD/VHS/Book example, let's say you want a bi-directional, many-to-one association between Borrow and DVD/VHS/Book (the "item"), then you map it on the Borrow side with the polymorphic @Any mechanism to item, and on the item side with a @OneToMany to Borrow.

BUT: on the latter property/getter, you also specify, explicitly, the join column to use, e.g. "ITEM_ID". That should force Hibernate to use only "ITEM_ID", and not (as I've seen) ITEM_ID + ITEM_TYPE that it infers by default from the @Any definition on the other side.

If you don't have a "Item" superclass for DVD/VHS/Book, you'll have to declare this in each class, something like:

@Entity
class Book {
    ...
    @OneToMany
    @JoinColumn(name="item_id")
    public List<Borrow> getBorrows() {
        return this.borrows;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文