Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent Nhibernate composite key mapping
    primarykey
    data
    text
    <p>I have tried to figure out this question for quite a long time. I have a hacky way to make it work.</p> <p>I just want to know if this is possible in Fluent NHibernate mapping.</p> <p>Say I have two tables for example:</p> <pre><code>Table ComissionLevel { Year, ComissionLevelID, ... other properties .... } primary key (Year,ComissionLevelID) Table ClientCommission { Year, ClientID, CommissionLevelID_1, CommissionLevelID_2, ... other properties ... } primary key (Year,ClientID) foreign key CommissionLevel1 (Year,CommissionLevelID_1) foreign key CommissionLevel2 (Year,CommissionLevelID_2) </code></pre> <p>Currently my mappings are as follow:</p> <pre><code>public ComissionLevelMap() { Schema("XXXX"); Table("ComissionLevel"); LazyLoad(); CompositeId() .KeyProperty(x =&gt; x.Year, set =&gt; { set.ColumnName("Year"); set.Access.Property(); } ) .KeyProperty(x =&gt; x.CommissionLevelID, set =&gt; { set.ColumnName("CommissionLevelID"); set.Length(10); set.Access.Property(); } ); HasMany&lt;ClientCommission&gt;(x =&gt; x.ClientCommissions) .Access.Property() .AsSet() .Cascade.AllDeleteOrphan() .LazyLoad() .Inverse() .Generic() .KeyColumns.Add("Year", mapping =&gt; mapping.Name("Year") .SqlType("NUMBER") .Nullable()) .KeyColumns.Add("CommissionLevelID_1", mapping =&gt; mapping.Name("CommissionLevelID_1") .SqlType("VARCHAR2") .Nullable() .Length(10)); HasMany&lt;ClientCommission&gt;(x =&gt; x.ClientCommission2s) .Access.Property() .AsSet() .Cascade.AllDeleteOrphan() .LazyLoad() .Inverse() .Generic() .KeyColumns.Add("Year", mapping =&gt; mapping.Name("Year") .SqlType("NUMBER") .Nullable()) .KeyColumns.Add("CommissionLevelID_2", mapping =&gt; mapping.Name("CommissionLevelID_2") .SqlType("VARCHAR2") .Nullable() .Length(10)); } public ClientCommissionMap() { Schema("XXXXX"); Table("ClientCommission"); LazyLoad(); CompositeId() .KeyProperty(x =&gt; x.ClientID, set =&gt; { set.ColumnName("ClientID"); set.Length(10); set.Access.Property(); } ) .KeyProperty(x =&gt; x.Year, set =&gt; { set.ColumnName("Year"); set.Access.Property(); } ); References(x =&gt; x.ComissionLevel1) .Class&lt;ComissionLevel&gt;() .Access.Property() .Cascade.None() .LazyLoad() .Insert() .Update() .Columns("Year", "CommissionLevelID_1"); References(x =&gt; x.ComissionLevel2) .Class&lt;ComissionLevel&gt;() .Access.Property() .Cascade.None() .LazyLoad() .Insert() .Update() .Columns("Year", "CommissionLevelID_2"); } </code></pre> <p>My problem now is whenever I create a CommissionLevel and assign ClientCommission to its collection, if I save them by call session.save(CommissionLevel) it will throw me an exception </p> <pre><code>&lt;Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index&gt;. </code></pre> <p>My question here is:</p> <ol> <li><p>Does NHibernate automatically save relationships? like:</p> <pre><code> ClientCommission commission = new ClientCommission{Year = 2012, ClientID =SomeGuid}; CommissionLevel newCommissionLevel = new CommissionLevel{Year = 2012, CommissionLevelID =NewCommissionLevelGuid}; newCommissionLevel.ClientCommission1s.Add(commission); newCommissionLevel.ClientCommission2s.Add(commission); CommissionLevelRepo.Save(newCommissionLevel); </code></pre> <p>When I call CommissionLevelRepo.Save(newCommissionLevel), should NHibernate will also update ClientCommission.ComissionLevel1 And ClientCommission.ComissionLevel2</p></li> </ol> <p>or do I have to say </p> <pre><code>ClientCommission.ComissionLevel1 = newCommissionLevel; ClientCommission.ComissionLevel2 = newCommissionLevel; </code></pre> <ol> <li>For the exception I got, it is because NHibernate doesn't generate correct column, it seems it will generate three Year columns. Cuz if I manually create two property called ComissionLevelID1 and CommissionLevelID2, disable the .Insert() and .Update() on ClientCommission it will save it properly.</li> </ol> <p>Can someone show me a proper way to map those two classes? </p> <p>Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload