NHibernate主键是1-1映射吗?
也许我只是设计错误,如果是这样,我非常乐意退出并以另一种方式掠夺它......
我目前有 2 个这样的表,它们有一个共享主键:
person - id (PK), name、created_date、...
person_details - person_id (PK)、age、height、...
现在,人员已创建,并且人员只能拥有一组详细信息。因此,最初我认为在映射 person_details 时,我会将 PK 分配给该人,因为它将由该人生成。
现在我对如何在数据库中创建此数据的第一个实例有点困惑,因为当我创建一个包含 PersonDetails 模型实例的 Person 模型时,此时两者都不会有 Id,因为它们还没有没有被创建。那么如何告诉 PersonDetails 模型在创建时从 Parent Person 模型获取其 Id 呢?
我只是疯了还是需要在 Person 模型中做一个简单的映射来告诉它在从自己的 Id 插入时推断出 PersonDetails Id ?我最终需要 PersonDetails 上的 Person_Id,因为如果在初始创建后进行更改,它们会通过 Ajax 在外部更新到人员...
Maybe I have just designed this incorrectly, and if so I am more than happy to bow out and loot at it another way...
I currently have 2 tables like this, which have a shared primary key:
person - id (PK), name, created_date, ...
person_details - person_id (PK), age, height, ...
Now the person gets created and a person can only have one set of details. So initially I thought that when mapping the person_details I would have the PK as being assigned, as it would be generated by the person.
Now im just a bit confused as to how I can create the first instance of this data in the database, as when I create a Person model, which contains an instance of PersonDetails model, neither will have an Id at this point, as they haven't been created. So how do I tell the PersonDetails model to take its Id from the Parent Person model when creating?
Am I just going mad or is there a simple mapping I need to do in the Person model to tell it to infer the PersonDetails Id when inserting from its own Id? I end up needing the Person_Id on the PersonDetails as they are updated externally to the person by Ajax if a change will be made after initial creation...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这些表共享一个 PK(我假设是个人发起的),那么您要么有一个 一对一,其中人员有详细信息,或 join,其中一个人有年龄、身高等,并且它们被映射到不同的表中。
读完这两章就可以了,里面有例子。
If the tables share a PK (which I assume is originated in person), then you have either a one-to-one, where a Person has a Detail, or a join, where a Person has Age, Height, etc and they are mapped into a different table.
Just read both chapters, they have examples.