ORM如何解决实体之间的双向关系(例如NHibernate)?
我正在为我的 RDBMS 课程写作业,我需要在非常简单的域(网络体育锦标赛)上执行 CRUD 操作。
学生必须使用 ADO.NET。我的问题是如何解决双向关系,例如1:m(每个冠军有很多场比赛,但每场比赛只属于一个确切的冠军)?在我看来,必须有一些技巧来做到这一点。
对我来说最有趣的部分是 - 像 EF 或 NHibernate 这样的 ORM 如何解决这种情况?
I'm writing a homework for my RDBMS class, I need to perform CRUD operations on quite simple domain, which is cyber sport championship.
Students are required to use ADO.NET. My question is how can I solve bidirectional relationship, for example 1:m (every championship has many matches, but every match belongs to only one exact championship)? It seems to me that there must be some technique for that.
And the most interesting part for me is - how does ORM like EF or NHibernate solve this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 NHibernate 中,它非常简单且直接。下面是域类的外观,后面是流畅的映射。这假设您将使用 NHibernate 来生成您的架构。如果您要映射旧数据库,则设置所使用的列名和表名很简单。
In NHibernate, it is quite simple and straight-forward. Here's how the domain classes would look, followed by fluent mappings. This assumes you would use NHibernate to generate your schema. If you are mapping a legacy database, it is simple to set the column names and table names used.
查看 Davy Briions 博客,了解如何构建您自己的数据访问层。他谈到了所有这些挑战。
Have a look at Davy Brions Blog about building your own Data Access Layer. He talks about all those sort of challenges.
对于像 Hibernate 中的多对多这样的东西,您可以定义关系。这是一个示例(参考是 这里:
从数据库方面来看,对于多对多关系,您通常会有一个链接表。
因此我们有:
PERSON_ADDRESS 表将包含 person_id 和 address_id 将两个实体链接在一起。因此,一个人可以有多个地址,并且一个给定的地址可能属于多个人或多个公司,
对于一对一的关系,这样就足够了:
在地址中,您可以有 person_id 列,但也可以有。给定 person_id 的地址记录很多,为您提供 1:m 能力。
For something like many-to-many with Hibernate, you define the relationship. Here's an example (reference is here:
From the database side itself, for many-to-many relationship you will usually have a link table.
So we'd have:
The PERSON_ADDRESS table would contain person_id and address_id to link the two entities together. So one person could have many addresses, and a given address could potentially belong to more than one person or company.
For a 1:m relationship, it's good enough to have this:
In address, you would have the person_id column, but there could be many address records for a given person_id, giving you the 1:m capability.
例如,在 DataObjects.Net 中,您可以编写以下内容来自动关联
Championship.Matches
实体设置和Match.Championship
持久字段。For example in DataObjects.Net you can write following to get automatically associated
Championship.Matches
entity set andMatch.Championship
persistent field.