FK是CompositeId的一部分时如何MapJPA一对多关系

nfzehxib  于 2023-01-02  发布在  Spring
关注(0)|答案(1)|浏览(139)

所以我有一个预先存在的服务实体,它有多个一对多关系,现在我需要再添加一个,但是我遇到了麻烦,我想这一定是因为多方使用了一个复合键。
我有Service.java及其新字段

@Column(name = "TRANSLATION_DV_ID")
private String translationDvId;

@OneToMany(cascade = CascadeType.All, fetch = FetchType.Eager, mappedBy = "service")
private List<Translation> translation;

以及

@IdClass(TranslationId.class)
public class Translation {
 @Id
 @Column(name = "TRANSLATION_DV_ID")
 private String translationDvId;

 @Id
 @Column(name = "LOCALE_CD")
 private String localeCd;

 @Column(name = "TRANSLATED_NAME")
 private String translatedName;

 @Column(name = "TRANSLATED_DESC")
 private String translatedDesc;

 @ManyToOne
 @JoinColumn(name = "TRANSLATION_DV_ID", insertable = false, updatable = false)
 private Service service;

测试数据是用sql脚本生成的。我输入了新数据并匹配了translationDvId。除了翻译关系外,所有数据都显示了正确的信息-每个服务总是有一个空的List<Translation>
我不确定我遗漏了什么,但这里有一个数据输入示例

INSERT INTO SCHEMA.SERVICE(SERVICE_CD, TRANSLATION_DV_ID, etc, etc)
VALUES ('servicePrimaryKey', '12345', 'etc, 'etc);

INSERT INTO SCHEMA.TRANSLATION(TRANSLATION_DV_ID, LOCALE_CD, TRANSLATED_NAME, TRANSLATED_DESC)
VALUES ('12345', 'English', 'Guardian', 'Cool stuff');
INSERT INTO SCHEMA.TRANSLATION(TRANSLATION_DV_ID, LOCALE_CD, TRANSLATED_NAME, TRANSLATED_DESC)
VALUES ('12345', 'Spanish', 'Guardia', 'Cosas interesantes');
6kkfgxo0

6kkfgxo01#

@JoinColumn有一个特殊属性,用于表示被引用表中何时存在复合PK- referencedColumnName

@OneToMany
@JoinColumn(name = "TRANSLATION_DV_ID", referencedColumnName = "TRANSLATION_DV_ID")
private List<Translation> translation;

相关问题