NHibernate: My composite table is not saved

by Jacobus Meintjes July 29, 2009 16:26

I was trying to save a many-to-many relationship set to the database, it saved my mapped objects. It did not however save my composite table (linked table) information.

 

The mappings and schema I was using for my classes (only extracts from the required sections):

Trust:

<bag name="Trustees" table="FinanceStore_TrustTrustee" cascade="all">
     <key>
          <column name="TrustId"/>
     </key>
     <many-to-many column="TrusteeId" class="FinanceStore.Model.Entities.Trustee, FinanceStore.Model"/>
</bag>

 

Trustee

<bag name="Trust" cascade="all"  table="FinanceStore_TrustTrustee" lazy="true">  
     <key>
         <column name="TrusteeId"/>
     </key>
     <many-to-many  column="TrustId" class="FinanceStore.Model.Entities.Trust, FinanceStore.Model"/>
</bag>

 

Database schema:

The mapping worked, in the sense of alowing me to retrieve my objects without creating a object between Trust and Trustee, which is the correct way to handle the objects. After some searching on google I found that I was using the wrong collection type mapping and should have used the <!--[endif]--> <idbag></idbag>configuration.

idbag explained by NHibernate: "NHibernate provides a feature that allows you to map many to many associations and collections of values to a table with a surrogate key. The <idbag> element lets you map a List (or Collection) with bag semantics"

 

In the end the mappings were changed to

Trust

Tags:

tinyMCE

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

About Me

Jacobus Meintjes
 
C# developer working with ASP.Net and/or Windows Forms.
Email Me

Cumulus

This will be shown to users with no Flash or Javascript.

Widget Twitter not found.

Root element is missing.X