简体   繁体   English

同一个表中不同项目的休眠映射

[英]Hibernate mapping for different items in the same table

I have the following three classes [Promotion, Product and Image] .我有以下三个类[Promotion, Product and Image] Promotion has an Image . Promotion有一个Image Product has list/collection of Image's . Product具有Image's列表/集合。 In the future there may be new classes that also has Image or Collection of Image's .将来可能会有新的类也有ImageImage's Collection of Image's

I would like to put all images in the same image table (eg:: 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.这意味着Promotion的图像和Product中的图像集合都将进入同一张表。

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) .例如,我不能在Image类中放置两个@OneToMany/@ManyToOne注释,因为这样在 Image table (eg:: image_table)会有两个不同的外键(Promotion_FK and Product_FK) (eg:: image_table) In the future if there are more classes that has image then I have to add additional @OneToMany/@ManyToOne annotations.将来如果有更多具有图像的类,那么我必须添加额外的@OneToMany/@ManyToOne注释。

  1. Is there a way to map this feature in Hibernate?有没有办法在 Hibernate 中映射此功能?

  2. If my design is not good, in the real world how do they store images that belongs to different classes in the same table?如果我的设计不好,在现实世界中他们如何将属于不同类的图像存储在同一张表中?

Try to use Unidirectional mapping then然后尝试使用单向映射

eg Promotion having Image , Product having Image 's, but not Vice Versa例如,促销形象产品形象反之亦然

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM