相同表中不同项目的Hibernate映射 [英] Hibernate mapping for different items in the same table

查看:67
本文介绍了相同表中不同项目的Hibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个班级[Promotion, Product and Image]. Promotion具有一个Image. Product具有Image's的列表/集合.将来可能会有新的类也具有ImageImage'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.

  1. 是否可以在Hibernate中映射此功能?

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆