Nhibernate: matching many-to-many relationships?

I have the following object model:

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; } } } 

And I have the following database schema:

 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 ) 

Now I am having serious problems figuring out how to map this to nhibernate (not fluent nhibernate). Basically, I want it to be displayed in such a way that when I do "session.Save (anOrganizer)", the data is sometimes written to two tables and sometimes written to three. For example, let's say we have OrganizerA, and it has three letters: email_1, email_2 and email_3. email_1 has EmailType.Primary, while the other two have EmailType.Secondary. Therefore, when we save OrganizerA, the following happens:

  • OrganizerA details are written to the Organizers table.
  • email_1, email_2 and email_3 are all written to the EmailAddresses table (if they do not already exist there)
  • And a new row is created in the Organizers_PrimaryKeys table, where id_ID_1 is written as PrimaryKeyID, and OrganizerA is written as OrganizerID (don't worry about PrimaryKeyTypeID. I'll find out later).

So how the hell am I comparing such a relationship? So far I have the following:

 <?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> 

I have no idea what to put in the Email.hbm.xml file to complete this many-to-many conditional relationship.

+4
source share
2 answers

You need to split between primary and secondary email into two different objects, because they are not the same entities. Add a one-to-one relationship to PrimaryEmail in the Organizer class and save the current SecondaryEmail Many-to-Many list.

 <?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> 

+2
source

email_1, email_2 and email_3 add to the table "Organizers" as properties

0
source

Source: https://habr.com/ru/post/1393589/


All Articles