ORM如何解决实体之间的双向关系(例如NHibernate)?

发布于 2024-08-14 13:01:22 字数 198 浏览 7 评论 0原文

我正在为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2024-08-21 13:01:22

在 NHibernate 中,它非常简单且直接。下面是域类的外观,后面是流畅的映射。这假设您将使用 NHibernate 来生成您的架构。如果您要映射旧数据库,则设置所使用的列名和表名很简单。

public class Championship {
  public virtual int Id { get; set; }
  public virtual IList<Match> Matches { get; set; }
}


public class Match {
  public virtual int Id { get; set; }
  public virtual Championship Champioship { get; set; }
}


public class ChampionshipMap : ClassMap<Championship> {
  public ChampionshipMap() {
    Id(x => x.Id);
    HasMany(x => x.Matches);
  }
}


public class MatchMap : ClassMap<Match> {
  public MatchMap () {
    Id(x => x.Id);
    References(x => x.Championship);
  }
}

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.

public class Championship {
  public virtual int Id { get; set; }
  public virtual IList<Match> Matches { get; set; }
}


public class Match {
  public virtual int Id { get; set; }
  public virtual Championship Champioship { get; set; }
}


public class ChampionshipMap : ClassMap<Championship> {
  public ChampionshipMap() {
    Id(x => x.Id);
    HasMany(x => x.Matches);
  }
}


public class MatchMap : ClassMap<Match> {
  public MatchMap () {
    Id(x => x.Id);
    References(x => x.Championship);
  }
}
糖粟与秋泊 2024-08-21 13:01:22

查看 Davy Briions 博客,了解如何构建您自己的数据访问层。他谈到了所有这些挑战。

Have a look at Davy Brions Blog about building your own Data Access Layer. He talks about all those sort of challenges.

风和你 2024-08-21 13:01:22

对于像 Hibernate 中的多对多这样的东西,您可以定义关系。这是一个示例(参考是 这里

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <set name="addresses" table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            class="Address"/>
    </set>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
    <set name="people" inverse="true" table="PersonAddress">
        <key column="addressId"/>
        <many-to-many column="personId"
            class="Person"/>
    </set>
</class>

从数据库方面来看,对于多对多关系,您通常会有一个链接表。

因此我们有:

PERSON
ADDRESS
PERSON_ADDRESS

PERSON_ADDRESS 表将包含 person_id 和 address_id 将两个实体链接在一起。因此,一个人可以有多个地址,并且一个给定的地址可能属于多个人或多个公司,

对于一对一的关系,这样就足够了:

PERSON
ADDRESS

在地址中,您可以有 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:

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <set name="addresses" table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            class="Address"/>
    </set>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
    <set name="people" inverse="true" table="PersonAddress">
        <key column="addressId"/>
        <many-to-many column="personId"
            class="Person"/>
    </set>
</class>

From the database side itself, for many-to-many relationship you will usually have a link table.

So we'd have:

PERSON
ADDRESS
PERSON_ADDRESS

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:

PERSON
ADDRESS

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.

一场信仰旅途 2024-08-21 13:01:22

例如,在 DataObjects.Net 中,您可以编写以下内容来自动关联 Championship.Matches 实体设置和 Match.Championship 持久字段。

[HierarchyRoot]
public class Championship : Entity
{
  [Field, Key]
  public int Id { get; set; }

  [Field, Association(PairTo="Championship")]
  public EntitySet<Match> Matches { get; private set; }
}

[HierarchyRoot]
public class Match : Entity
{
  [Field, Key]
  public int Id { get; set; }

  [Field]
  public Championship Championship { get; set; }
}

For example in DataObjects.Net you can write following to get automatically associated Championship.Matches entity set and Match.Championship persistent field.

[HierarchyRoot]
public class Championship : Entity
{
  [Field, Key]
  public int Id { get; set; }

  [Field, Association(PairTo="Championship")]
  public EntitySet<Match> Matches { get; private set; }
}

[HierarchyRoot]
public class Match : Entity
{
  [Field, Key]
  public int Id { get; set; }

  [Field]
  public Championship Championship { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文