休眠一对一(在外键上)与一对一(在主键上) [英] hibernate one-to-one (on foreign key) vs one-to-one (on primary key)

查看:196
本文介绍了休眠一对一(在外键上)与一对一(在主键上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在外键上使用一对一关联或者在主键上使用一对一关联有什么好处有疑问,我已经阅读了hibernate的文档: http://docs.jboss.org/hibernate /orm/3.3/reference/en/html/associations.html#assoc-unidirectional-121
有人可以告诉我详细的使用第一或第二个实施的好处吗?

另一个问题,我有一个用户和周长
(用户---->周长)之间的一对一的单向关系,我想用基于外键。
我可以改变方向(User< ---- Perimeter),所以表格用户保持不变?
我认为这是不合理的(perimeter.getUser()!!!),但它在技术上是可能的?

解决方案

除了JB Nizet提到的优点之外,还有一个人告诉我,基于外键的一对一比较灵活。让我们考虑我对外键(用户< ---外围)使用一对一,如果应用程序的需求变化需要多个用户的任何外围,如果我共享主键,我会重构要做得更多。但是,当我使用外键时,我所做的只是放松唯一的约束;)

i have questions about what is the benefits of using a one-to-one association on foreign key or a one-to-one association on primary key, I've read the documentation of hibernate available at : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html#assoc-unidirectional-121 can someone tell me with details the plus of using the first or the second implementation?

another question, i'have a one-to-one unidirectional relationship between User and Perimeter (User ----> Perimeter) and i want to use the association based on the foreign key. could i reverse the direction into (User <---- Perimeter) so the table User stay intact? I think that is not reasonable (perimeter.getUser() !!!) but it's technically possible?

解决方案

In addition to advantages mentioned by JB Nizet and thanks to another person told me that the one-to-one based on foreign key is more flexible. let's consider that i use a one-to-one on foreign key (User <--- Perimeter) , if the requirements of the application changes to need multiple Users for any Perimeter, If i shared the primary key, I'll have much more refactoring to do. But as i used the foreign key, all i do is relax the unique constraint ;)

这篇关于休眠一对一(在外键上)与一对一(在主键上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆