ORM - MappedSuperclass 上的多对多关系

发布于 2024-12-29 12:29:51 字数 692 浏览 0 评论 0原文

我有一个类如下:

AnimalClass [Id, Name, Set<Tag>]
|
+-- FishClass [FishSpecific]
+-- MammalClass [MammalSpecific]

Tag [Name]

所以任何动物都可以有任意数量的关联标签。

为此,我在 AnimalClass 中使用:

@JoinTable(name="Animal_Tag")
@JoinColumn(name="animal_id", referencedColumnName="id", nullable=false)
@OneToMany(cascade=CascadeType.ALL)
@Getter
protected Set<Tag> tags = new HashSet<Tag>();

我的问题是,Hibernate 将 mn 表创建为:

 Animal_Tag [FishClass_id, MammalClass_id, Tag_id]. 

我更希望有某种枚举为:

 Animal_Tag [Animal_id, AnimalTypeEnumeration[ Fish | Mammal ], Tag_id].

谢谢!

I have a classes as follows:

AnimalClass [Id, Name, Set<Tag>]
|
+-- FishClass [FishSpecific]
+-- MammalClass [MammalSpecific]

Tag [Name]

So any animal can have any number of associated tags.

For that I use in AnimalClass:

@JoinTable(name="Animal_Tag")
@JoinColumn(name="animal_id", referencedColumnName="id", nullable=false)
@OneToMany(cascade=CascadeType.ALL)
@Getter
protected Set<Tag> tags = new HashSet<Tag>();

My problem is that, Hibernate creates the m-n table as:

 Animal_Tag [FishClass_id, MammalClass_id, Tag_id]. 

I would prefer to have some kind of enumeration as:

 Animal_Tag [Animal_id, AnimalTypeEnumeration[ Fish | Mammal ], Tag_id].

Thanks!

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

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

发布评论

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

评论(3

浅暮の光 2025-01-05 12:29:51

您想要的表结构将有一个可以指向不同表的外键,具体取决于另一个字段的值。我认为任何数据库都不允许这样做。

ORM 的多态性总是很棘手。最好的办法就是尽可能避免它。否则,也许您可​​以在 Animal 上使用 @Inheritance(strategy=InheritanceType.JOINED) ?这将产生如下表结构:

TABLE Animal
 - id (primary key)

TABLE Fish
 - id (foreign key -> Animal)
 - fins
 - scales

TABLE Mammal
 - id (foreign key -> Animal)
 - mammaries 

The table structure that you want would have a foreign key that can point to different tables, depending on the value of another field. I don't think any DB allows that.

Polymorphism with ORM is always tricky. Best thing to do is just avoid it if you can. Otherwise, maybe you could use @Inheritance(strategy=InheritanceType.JOINED) on Animal? This would result in a table structure like this:

TABLE Animal
 - id (primary key)

TABLE Fish
 - id (foreign key -> Animal)
 - fins
 - scales

TABLE Mammal
 - id (foreign key -> Animal)
 - mammaries 
妄断弥空 2025-01-05 12:29:51

我怀疑你不能用映射的超类来做到这一点。

关于映射超类的问题是它没有定义持久类型。它为持久类型定义了一种模板。每次定义一个带有 @Entity 注释的子类时,您都会创建模板的一个实例,但在数据模型中,这些类型之间没有关系。使用映射的超类几乎是将给定字段集复制并粘贴到新实体类中的快捷方式。

所以,就数据模型而言,不可能有animal_id,因为不存在animal这样的类型。数据库中仅存在鱼类和哺乳动物。

您可以使 AnimalClass 成为一个实体而不是映射的超类吗?如果您使用每类一个表的继承策略,则无需为其创建表。但它将使animal成为一种类型,这意味着ORM将能够使用animal_id。

I suspect you can't do that with a mapped superclass.

The thing about a mapped superclass is that it doesn't define a persistent type. It defines a sort of template for a persistent types. Every time you define a subclass of it which is annotated @Entity, you create an instance of the template, but in the data model, there is no relationship between those types. The use of the mapped superclass is almost a shortcut for a copy-and-paste of a given set of fields into the new entity class.

So, as far as the data model is concerned, there is no possible animal_id, because there is no such type as animal. Only fish and mammal exist in the database.

Can you make AnimalClass an entity instead of a mapped superclass? If you use the table-per-class inheritance strategy, you won't need to create a table for it. But it will make animal a type, which means the ORM will be able to use an animal_id.

平生欢 2025-01-05 12:29:51

可以对 AnimalClass 使用“每个子类带有鉴别器的表”继承吗?在休眠的情况下,会导致hiredaly:

AnimalClass [Id, AnimalTypeEnumeration (discriminator), Name]
|
+-- FishClass [FishSpecific]
+-- MammalClass [MammalSpecific]
Animal_Tag [Animal_id, Tag_id]

May be use "table per subclass wih discriminator" inheritance for AnimalClass? In case of hibernate it result fowing hirecaly:

AnimalClass [Id, AnimalTypeEnumeration (discriminator), Name]
|
+-- FishClass [FishSpecific]
+-- MammalClass [MammalSpecific]
Animal_Tag [Animal_id, Tag_id]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文