I'm trying to figure how to do the mapping for this or if it is even possible. I have a many-to-many relationship between two tables (for simplicity's sake I've reduced the tables down to pertinent information):
Code:
CREATE TABLE Person (
PersonId int identity(1,1) NOT NULL
CONSTRAINT PK_PERSON_PERSONID PRIMARY KEY,
LastName varchar(50) NOT NULL,
FirstName varchar(50) NOT NULL
)
Code:
CREATE TABLE Address (
AddressId int identity(1,1) NOT NULL
CONSTRAINT PK_ADDRESS_ADDRESSID PRIMARY KEY,
Line1 varchar(50) NOT NULL
)
The addresses are linked to the Person via a link table:
Code:
CREATE TABLE PersonAddress (
PersonAddressId int identity(1,1) NOT NULL
CONSTRAINT PK_PERSONADDRESS_PERSONADDRESSID PRIMARY KEY,
PersonId int NOT NULL
CONSTRAINT FK_PERSONADDRESS_PERSONID FOREIGN KEY REFERENCES Person (PersonId),
AddressId int NOT NULL
CONSTRAINT FK_PERSONADDRESS_ADDRESSID FOREIGN KEY REFERENCES Address (AddressId),
ContactDescriptionId int NULL
CONSTRAINT FK_PERSONADDRESS_CONTACTDESCRIPTIONID FOREIGN KEY REFERENCES ContactDescription (ContactDescriptionId))
The Contact Description table is just a simple lookup table:
Code:
CREATE TABLE ContactDescription (
ContactDescriptionId int identity(1,1) NOT NULL
CONSTRAINT PK_CONTACTDESCRIPTION_CONTACTDESCRIPTIONID PRIMARY KEY,
Description varchar(30) NOT NULL
)
My question is, when using a many-to-many relationship between Person and Address, I would like to attach the contact description class as a property of the Address. This is done because the addresses are reused in relation to other tables (for instance there is a DivisionAddress link table, etc) so the contact description for a specific address might be one thing when linked to Person A but might be completely different when the same address is linked to Division A.
Here are the mappings I am currently using:
Code:
<class name="Person" table="Person">
<id name="Id">
<column name="PersonId" sql-type="Int32" not-null="true"/>
<generator class="native" />
</id>
<property name="LastName" />
<property name="FirstName" />
<bag name="AddressEntries" lazy="true" cascade="all" table="PersonAddress">
<key column="PersonId"/>
<many-to-many class="Address" column="AddressId" />
</bag>
</class>
Code:
<class name="Address" table="Address">
<id name="Id">
<column name="AddressId" sql-type="Int32" not-null="true"/>
<generator class="native" />
</id>
<property name="Line1" />
</class>
Can this be done? If so how would the mapping for it work. Thanks.