ninjasense
ninjasense

Reputation: 13856

nHibernate mapping with composite key and collection

I am having trouble trying to map this object that contains a collection of guids. I have the mapping working with no errors, however I keep getting empty guids inserted into OrganizationId in the OrganizationAdvertistments table. I was wondering how to map this set correctly?

<?xml version="1.0" encoding="utf-8" ?>

<class name="Advertisement" table="Advertisement" lazy="false">
    <id name="Id">
        <generator class="guid"></generator>
    </id>
    <property name="DateStart"/>
    <property name="DateExpired"/>
    <property name="Title"/>
    <property name="Body"/>

    <set name="OrganzationIds" table="OrganizationAdvertisements" lazy="true">

        <key>
            <column name="AdvertisementId"></column>
        </key>
        <element  column="OrganizationId" type="Guid" />


    </set>


</class>

Upvotes: 1

Views: 280

Answers (1)

Rashad
Rashad

Reputation: 103

Are you gonna have many to many relationship between Advertistment and Organization? if yes you've got had another class: Organization and set many to many relationship between these two classes and there is no need to map OrganzationIds. Your class would be somthing like this:

class Advertisement
{
 //other properties
 public virtual ISet<Organization> Organizations {ge;set}
}

and your map:

 <class name="Advertisement" table="Advertisement" lazy="false">  
        <id name="Id">  
            <generator class="guid"></generator>  
        </id>  
        <property name="DateStart"/>  
        <property name="DateExpired"/>  
        <property name="Title"/>  
        <property name="Body"/>  

         <set name="Organizations"
               table="OrganizationAdvertisements"
               lazy="true">
            <key column="AdvertisementId" />
            <many-to-many class="Namespace.Organization"
                          column="OrganizationID" />
          </set>
    </class>  

Upvotes: 2

Related Questions