多对多映射,联接表中有额外的列 [英] Many-to-many mapping with extra columns in join table
问题描述
这是我希望拥有的域:
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; }
}
这是我拥有的架构:
CREATE TABLE People (
PersonId INT PRIMARY KEY
);
CREATE TABLE Certificates (
CertificateId INT PRIMARY KEY
);
CREATE TABLE CertificatesAcquiredByPeople (
PersonId INT,
CertificatedId INT,
DateAcquired DATETIME
);
这是人为设计的架构和域,但与我正在使用的东西几乎相同.我目前正在通过编写第三个域实体来表示CertificatesAcquiredByPeople表来进行工作,但这对我来说真的很奇怪.
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.
我该如何使用NHibernate映射它?我相信hbm文件中的component标签应该可以实现我想要的功能,但是我不太清楚.
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.
我的域是否因为我的证书类具有DateAcquired属性而无法使用?日期确实只是拥有证书的人的问题.
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.
我现在已经更改了域模型,以反映需要一个新实体.现在,对于映射,我需要3个(对于每个实体)映射,还是可以对2个(对于Person和Certificate)进行映射?
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)?
推荐答案
通过设计,如果除了中间(中间)表中表示的那对FK之外绝对没有其他内容,NHibernate仅支持隐式多对多映射拥有多对多关系.
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.
前段时间,Billy McCafferty在博客上写了这个确切的问题"(自从其BY DESIGN以来就不是一个真正的问题)...
Some time ago, Billy McCafferty blogged about this exact 'issue' (not really an issue since its BY DESIGN)...