休眠一对一(在外键上)与一对一(在主键上)

编程入门 行业动态 更新时间:2024-10-28 13:22:56
本文介绍了休眠一对一(在外键上)与一对一(在主键上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对在外键上使用一对一关联或者在主键上使用一对一关联有什么好处有疑问,我已经阅读了hibernate的文档: docs.jboss/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 : docs.jboss/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 ;)

更多推荐

休眠一对一(在外键上)与一对一(在主键上)

本文发布于:2023-10-26 14:37:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在外   主键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!