简体   繁体   English

一个映射实体中的外键为空

[英]Foreign key is null in one mapping entity

I was wondering why when I mapping two entity with mappedBy , in target class I have ID my member class, but in my member class I don't have target class ID . 我想知道为什么当我用mappedBy映射两个实体时,在目标类中我有成员类ID ,但是在成员类中我没有目标类ID Example code: 示例代码:

 //class Onion
@OneToOne(mappedBy = "onion", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Potatoe potatoe;

//class Potatoe
@OneToOne(cascade = CascadeType.ALL)
private Onion onion;

And everything works fine, I have a bidirectional relationship. 一切正常,我有双向关系。 But on DB I noticed that class Onion doesn't has ID from Potatoe , but Potatoe has Onion ID . 但是在DB上我注意到类Onion没有Potatoe ID,但是Potatoe具有Onion ID I know that when I want take potatoe object from onion object, persistence querying database for potatoe which has onion ID. 我知道,当我想要从洋葱对象中获取potatoe对象时,持久性查询数据库以获取具有洋葱ID的potatoe。

Why is it looking that? 为什么看起来呢? Is it possible to have potatoe ID in onion table? 洋葱表中可以有土豆ID吗?

Eclipslink 2.5.0 PostgreSQL 9.1 Eclipslink 2.5.0 PostgreSQL 9.1

It looks this way, with only a single foreign key because you have only a single (bidirectional) relationship controlled by the Potatoe->Onion mapping, so it is represented with the Potatoe table having a foreign key to Onion. 它看起来是这样,因为只有一个外键,因为您只有一个马铃薯(马铃薯)->洋葱映射控制的(双向)关系,所以它用具有洋葱外键的马铃薯表来表示。 If you want another foreign key from Onion to potatoe, it would mean you are asking for a relationship that can be entirely independent of the Potatoe->Onion relationship. 如果您想要从洋葱到土豆的另一个外键,则意味着您正在要求一种可以完全独立于土豆与洋葱关系的关系。 This is easily done by just removing the mappedBy designator, which would make the Onion->Potatoe mapping completely independent and use its own foriegn key. 只需删除mapledBy指示符即可轻松完成,这将使Onion-> Potatoe映射完全独立并使用其自己的foriegn键。 Your model then would have two unidirectional mappings that must both be maintained. 这样,您的模型将具有两个必须同时维护的单向映射。

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

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