@OneToOne unidirectional and bidirectional

坚强是说给别人听的谎言 提交于 2019-11-27 20:49:21

Because you didn't understand how a bidirectional OneToOne is mapped. You don't need two foreign keys. A single one is sufficient to perform a join between both tables, in both directions:

select a.* from address a inner join customer c on c.addressId = a.id;
select c.* from customer c inner join address a on c.addressId = a.id;

The fact that the association is unidirectional or bidirectional doesn't change how the tables are linked together. It just changes the mapping, and allows navigating through the association in both directions.

In a bidirectional association, you always have an owning side (which tells how the association is mapped, using which join column), and an inverse side, where you just say: I'm the other side of the association that is mapped by this field (the field in the mappedBy attribute value) in the target entity.

I just had an experiment on this. If you just do this below, both tables will have foreign keys:

@OneToOne
private Address43 address;

@OneToOne
private Customer44 customer;

Now you don't need to do the join. I am not sure if it is a good practice or not. Just make sure if you don't have CASCADES.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!