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!
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
AnimalClassan 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.