简体   繁体   English

与主表不是一对一关系时如何定义SecondaryTable

[英]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的关系PartInfoPartLabel

暂无
暂无

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

相关问题 Spring Boot / Hibernate:如何定义一对一的单向关系 - Spring Boot/Hibernate: How to define one-to-one unidirectional relationship 与Hibernate上的@SecondaryTable有多对一的关系 - Many to one relationship with @SecondaryTable on Hibernate 当“父”表具有复合 PK 时,如何在 JPA 中建模一对一关系? - How to model a one-to-one relationship in JPA when the “parent” table has a composite PK? JPA - 无法获得以一对一关系生成的主键 - JPA - Could not get primary key generated in a one-to-one relationship 如何确保 hibernate 5 在与共享主键的一对一关系中以正确的顺序持续存在 - How to make sure hibernate 5 persists in the correct order in a one-to-one relationship with shared primary key 如何将嵌套的 JSON 保存到具有一对一关系且共享主键的实体中? - How to save nested JSON into entities with one-to-one relationship that shares primary key? 在JPA中共享主键为一对一关系的情况下如何访问对象? - How to access object in case of shared primary key as one-to-one relationship in JPA? 如何在非主键上使用休眠注释设置单向一对一关系? - how to set unidirectional one-to-one relationship using hibernate annotation on a non-primary key? 如何正确级联在Hibernate 3.6中保存主键上的一对一双向关系 - How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6 如何坚持一对一的关系? (没有框架) - How to persist a one-to-one relationship ? (no framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM