ORM - MappedSuperclass 上的多对多关系
我有一个类如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的表结构将有一个可以指向不同表的外键,具体取决于另一个字段的值。我认为任何数据库都不允许这样做。
ORM 的多态性总是很棘手。最好的办法就是尽可能避免它。否则,也许您可以在 Animal 上使用
@Inheritance(strategy=InheritanceType.JOINED)
?这将产生如下表结构: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:我怀疑你不能用映射的超类来做到这一点。
关于映射超类的问题是它没有定义持久类型。它为持久类型定义了一种模板。每次定义一个带有
@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.可以对 AnimalClass 使用“每个子类带有鉴别器的表”继承吗?在休眠的情况下,会导致hiredaly:
May be use "table per subclass wih discriminator" inheritance for AnimalClass? In case of hibernate it result fowing hirecaly: