在连接表中使用额外列进行多对多映射

时间:2022-06-02 00:19:17

Here is the domain that I wish to have:

这是我希望拥有的域名:

public class Person
{
    public int Id { get; set; }
    public IList<AcquiredCertificate> AcquiredCertificates { get; set; }
}

public class AcquiredCertificate
{
    public Person Acquirer { get; set; }
    public Certificate Certificate { get; set; }
    public DateTime DateAcquired;
}

public class Certificate
{
    public int Id { get; set; }     
}

And this is the schema that I have:

这是我的架构:

CREATE TABLE People (
    PersonId INT PRIMARY KEY
);

CREATE TABLE Certificates (
    CertificateId INT PRIMARY KEY
);

CREATE TABLE CertificatesAcquiredByPeople (
    PersonId INT,
    CertificatedId INT,
    DateAcquired DATETIME
);

It's a contrived schema and domain but it's pretty much the same as something that I am working with. I currently have it working by writing a 3rd domain entity to represent the CertificatesAcquiredByPeople table but that really seems strange to me.

这是一个人为的架构和域,但它与我正在使用的东西几乎相同。我目前通过编写第三个域实体代表CertificatesAcquiredByPeople表来工作,但这对我来说真的很奇怪。

How would I map this using NHibernate? I believe the component tag in the hbm file should do what I want, but I can't quite figure it out.

我如何使用NHibernate进行映射?我相信hbm文件中的组件标签应该做我想要的,但我无法弄明白。

Is my domain out of whack because I have a DateAcquired property on my Certificate class? The date really is only a concern of a Person that has a certificate.

我的域名是不是因为我的证书类上有DateAcquired属性?该日期实际上只是具有证书的人员的关注点。

[Edit]

I've altered the domain model now to reflect that a new entity is needed. Now for the mapping do I need 3 (for each entity) mappings or can I do it with 2 (for Person and Certificate)?

我现在改变了域模型,以反映需要一个新的实体。现在对于映射,我需要3个(对于每个实体)映射还是可以用2(对于Person和Certificate)进行映射?

5 个解决方案

#1


By design, NHibernate only supports the implicit many-to-many mapping if there is absolutely NOTHING other than the pair of FKs represented in the intermediate (middle) table that holds the many-to-many relationship.

根据设计,如果除了在中间(中间)表中表示的具有多对多关系的FK对之外,NHibernate仅支持隐式多对多映射。

Some time ago, Billy McCafferty blogged about this exact 'issue' (not really an issue since its BY DESIGN)...

前一段时间,Billy McCafferty在博客中写到了这个确切的“问题”(自BY DESIGN以来并不是真正的问题)......

http://devlicio.us/blogs/billy_mccafferty/archive/2008/07/11/when-to-use-many-to-one-s-vs-many-to-many-with-nhibernate.aspx

#2


I think you need 3, if you are ever going to get at the DateTime value.

如果你想获得DateTime值,我认为你需要3。

#3


Your implementation is exactly correct. Your join table includes the two key fields (making a composite primary key for the table) and the datetime field is superfluous to that. It is in fact an extra attribute on the join table and for that you need an entity.

您的实施完全正确。您的连接表包括两个关键字段(为表创建复合主键),而datetime字段对此是多余的。它实际上是连接表上的额外属性,因此您需要一个实体。

On a UML class diagram it would show up as an attribute on the join too.

在UML类图上,它也将显示为连接上的属性。

#4


I would rename CertificatesAcquiredByPeople something
like CertificatesAcquiredEvent (it implies that there is more than one key and a datetime)

我会将CertificatesAcquiredByPeople重命名为CertificatesAcquiredEvent(它意味着有多个键和一个日期时间)

I agree it needs to be a seperate Entity as far as NHibernate is concerned.

我同意就NHibernate而言,它需要是一个单独的实体。

#5


Re: your updated Q, you will need three mappings, one for each of the three entities that now participate in the pair of one-to-many relations.

Re:你更新的Q,你需要三个映射,一个用于现在参与这对一对多关系的三个实体中的每一个。

#1


By design, NHibernate only supports the implicit many-to-many mapping if there is absolutely NOTHING other than the pair of FKs represented in the intermediate (middle) table that holds the many-to-many relationship.

根据设计,如果除了在中间(中间)表中表示的具有多对多关系的FK对之外,NHibernate仅支持隐式多对多映射。

Some time ago, Billy McCafferty blogged about this exact 'issue' (not really an issue since its BY DESIGN)...

前一段时间,Billy McCafferty在博客中写到了这个确切的“问题”(自BY DESIGN以来并不是真正的问题)......

http://devlicio.us/blogs/billy_mccafferty/archive/2008/07/11/when-to-use-many-to-one-s-vs-many-to-many-with-nhibernate.aspx

#2


I think you need 3, if you are ever going to get at the DateTime value.

如果你想获得DateTime值,我认为你需要3。

#3


Your implementation is exactly correct. Your join table includes the two key fields (making a composite primary key for the table) and the datetime field is superfluous to that. It is in fact an extra attribute on the join table and for that you need an entity.

您的实施完全正确。您的连接表包括两个关键字段(为表创建复合主键),而datetime字段对此是多余的。它实际上是连接表上的额外属性,因此您需要一个实体。

On a UML class diagram it would show up as an attribute on the join too.

在UML类图上,它也将显示为连接上的属性。

#4


I would rename CertificatesAcquiredByPeople something
like CertificatesAcquiredEvent (it implies that there is more than one key and a datetime)

我会将CertificatesAcquiredByPeople重命名为CertificatesAcquiredEvent(它意味着有多个键和一个日期时间)

I agree it needs to be a seperate Entity as far as NHibernate is concerned.

我同意就NHibernate而言,它需要是一个单独的实体。

#5


Re: your updated Q, you will need three mappings, one for each of the three entities that now participate in the pair of one-to-many relations.

Re:你更新的Q,你需要三个映射,一个用于现在参与这对一对多关系的三个实体中的每一个。