Nhibernate: Mapping a conditional many-to-many relationship?
我有以下对象模型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public class Organizer { private int id; public virtual int Id { get { return id; } set { id = value; } } private string fullName =""; public virtual string FullName { get { return fullName; } set { fullName = value; } } private List<Email> emails = new List<Email>(); public virtual List<Email> Emails { get { return emails; } set { emails = value; } } } public enum EmailType { Primary, Secondary } public class Email { private int iD; public virtual int ID { get { return iD; } set { iD = value; } } private string emailAddress =""; public virtual string EmailAddress { get { return emailAddress; } set { emailAddress = value; } } private EmailType emailType = EmailType.Primary; public virtual EmailType EmailType { get { return emailType; } set { emailType = value; } } private List<Organizer> organizers; public virtual List<Organizer> Organizers { get { return organizers; } set { organizers = value; } } } |
我有以下数据库模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | CREATE TABLE [dbo].[EmailAddresses]( [ID] [int] IDENTITY(1,1) NOT NULL, [EmailAddress] [nvarchar](550) NULL ) CREATE TABLE [dbo].[Organizers]( [ID] [int] IDENTITY(1,1) NOT NULL, [FullName] [nvarchar](550) NULL ) CREATE TABLE [dbo].[Organizers_PrimaryKeys]( [ID] [int] IDENTITY(1,1) NOT NULL, [PrimaryKeyID] [int] NULL, [PrimaryKeyTypeID] [int] NULL, [OrganizerID] [int] NULL ) |
现在,我有一个很严重的问题要解决,如何用NHibernate(不是流利的NHibernate)来映射这个问题。基本上,我希望它被映射,这样当我执行"session.save(anororganizer)"时,数据有时被写入两个表,有时被写入三个表。例如,假设我们有Organizera,他有三封电子邮件:email_1、email_2和email_3。email_1具有emailtype.primary,而其他两个具有emailtype.secondary。因此,当我们保存Organizera时,会发生以下情况:
那么我该如何描绘这种关系呢?到目前为止,我有以下几点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BLL" namespace="BusinessLogic"> <class name="Organizer" table="Organizers"> <id name="Id"> <column name="ID"/> <generator class="native" /> </id> <property name="FullName"> <column name="FullName"/> </property> <bag name="Emails" table="Organizers_PrimaryKeys" inverse="false" cascade="all" lazy="true"> <key column="Id"/> <many-to-many class="Email" /> </bag> </class> </hibernate-mapping> <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BLL" namespace="BusinessLogic"> <class name="Email" table="EmailAddresses"> <id name="ID"> <column name="ID"/> <generator class="native" /> </id> <property name="EmailAddress" unique="true" type="string"> <column name="EmailAddress"/> </property> </class> </hibernate-mapping> |
我不知道在email.hbm.xml中放什么来完成这个"有条件的"多对多关系。
您需要将主邮件和次邮件分离到两个不同的对象,因为它们不是同一种实体。将Organizer类添加到PrimaryEmail的一对一关系,并保留当前的多对多第二电子邮件列表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BLL" namespace="BusinessLogic"> <class name="Organizer" table="Organizers"> <id name="Id"> <column name="ID"/> <generator class="native" /> </id> <property name="FullName"> <column name="FullName"/> </property> <one-to-one name="PrimaryEmail" class="PrimaryEmail" /> <bag name="Emails" table="EmailAddresses" inverse="false" cascade="all" lazy="true"> <key column="Id"/> <many-to-many class="SecondaryEmail" /> </bag> </class> <class name="PrimaryEmail" table="Organizers_PrimaryKeys"> <id name="ID"> <column name="ID"/> <generator class="native" /> </id> <property name="EmailAddress" unique="true" type="string"> <column name="EmailAddress"/> </property> </class> <class name="SecondaryEmail" table="EmailAddresses"> <id name="ID"> <column name="ID"/> <generator class="native" /> </id> <property name="EmailAddress" unique="true" type="string"> <column name="EmailAddress"/> </property> </class> |
电子邮件1、电子邮件2和电子邮件3作为属性附加到组织者表中