相同表中不同项目的Hibernate映射 [英] Hibernate mapping for different items in the same table
问题描述
我有以下三个班级[Promotion, Product and Image]
. Promotion
具有一个Image
. Product
具有Image's
的列表/集合.将来可能会有新的类也具有Image
或Image's
的集合.
I have the following three classes [Promotion, Product and Image]
. Promotion
has an Image
. Product
has list/collection of Image's
. In the future there may be new classes that also has Image
or Collection of Image's
.
我想将所有图像放在同一张图像表(eg:: image_table)
中.这意味着Promotion
的图像和Product
中的图像集合都将放在同一张表中.
I would like to put all images in the same image table (eg:: image_table)
. It means that the image of Promotion
and the collection of images in Product
are all going to go into the same table.
例如,我不能在Image
类中放置两个@OneToMany/@ManyToOne
注释,因为这样在图像表(eg:: image_table)
中将有两个不同的外键(Promotion_FK and Product_FK)
.将来,如果有更多具有图像的类,那么我必须添加其他@OneToMany/@ManyToOne
批注.
I can't put for instance two @OneToMany/@ManyToOne
annotations in Image
class because then there will be two different foreign keys (Promotion_FK and Product_FK)
in Image table (eg:: image_table)
. In the future if there are more classes that has image then I have to add additional @OneToMany/@ManyToOne
annotations.
-
是否可以在Hibernate中映射此功能?
Is there a way to map this feature in Hibernate?
如果我的设计不好,在现实世界中它们如何存储属于不同类的图像?
If my design is not good, in the real world how do they store images that belongs to different classes?
推荐答案
然后尝试使用单向映射
例如 促销具有图片,产品具有图像,但反之亦然
e.g. Promotion having Image, Product having Image's, but not Vice Versa
这篇关于相同表中不同项目的Hibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!