[英]How to define the SecondaryTable when it is not One-To-One relationship with the primary table
IN a project, we have to map one Entity to two tables. 在一个项目中,我们必须将一个实体映射到两个表。 table part_info stores the commodity info, table part_label stores the label that will be shown on screen. table part_info存储商品信息,table part_label存储将在屏幕上显示的标签。
Table part_info
id part_no part_description
Table part_label
id part_no_label part_description_label
then in our Entity 然后在我们的实体
@Entity
@Table(name="part_info")
@SecondaryTable(name="part_label",pkJoinColumns= {@PrimaryKeyJoinColumn(name="id",referencedColumnName="id")})
public class PartInfo implements Serializable {
@id
private int id;
@Column(name = "part_no")
private String partNo;
@Column(name = "part_description")
private String partDesc;
@Column(table="part_label",name="part_no_label")
private String partNoLabel;
@Column(table="part_label",name="part_description_label")
private String partDescLabel;
....
}
but this will only map one record of part_info to one record of part_label. 但这只会将part_info的一条记录映射到part_label的一条记录。
In our case, different part info may use the same part label. 在我们的例子中,不同的零件信息可能使用相同的零件标签。 eg we will use label retrieved from part_label as below: 例如,我们将使用从part_label检索的标签,如下所示:
Part#
Part Desc
but we retrieved many part info from part_info table 但是我们从part_info表中检索了很多零件信息
PA123 AAA
PA123 DDD
PA234 BBB
PA345 CCC
Then in UI, we will show as: 然后在用户界面中,我们将显示为:
Part#:PA123 Part Desc:AAA
Part#:PA123 Part Desc:DDD
If use above @SecondaryTabel, each part_info and each part_label are One-To-One relationship, if need to retrieve two records of parts PA123, also need to retrieve same label twice. 如果使用@SecondaryTabel以上,每个part_info和每个part_label都是一对一的关系,如果需要检索PA123的两个部分记录,还需要检索两次相同的标签。
Is there any way that one part label can map to many part info? 是否有任何方式可以将一个零件标签映射到许多零件信息?
You misunderstand SecondaryTable, since it is only ever for one to one situation, and if the object is deleted then the row from both tables is deleted. 您误解了SecondaryTable,因为它只适用于一对一的情况,如果删除该对象,则删除两个表中的行。 There is no "reuse" of entries in the secondary table ... since the secondary table has a FK to the primary table!!! 辅助表中没有条目的“重用”...因为辅助表具有到主表的FK!
If you want to reuse some data then make that a separate entity. 如果要重用某些数据,请将其作为单独的实体。
我认为最合理的解决办法是模拟PartLabel
作为一个独立的实体,并创建一个ManyToOne
的关系PartInfo
到PartLabel
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.