Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate 3.2 mapping by code. When we create a bag relation, what mean rm.OneToMany?
    primarykey
    data
    text
    <p>I use nhibernate 3.2 mapping by code and I've a strange behaviour.</p> <p>If I simplify my project, I have 2 tables :</p> <pre><code>[Serializable] public class Intervention { public Intervention() { ReponsePointVerification = new List&lt;ReponsePointVerification&gt;(); } #region Public Properties public virtual int Id { get; set; } public virtual IList&lt;ReponsePointVerification&gt; ReponsePointVerification { get; set; } } public class InterventionMap : ClassMapping&lt;Intervention&gt; { public InterventionMap() { Id&lt;int&gt;(x =&gt; x.Id, map =&gt; { map.Generator(NHibernate.Mapping.ByCode.Generators.Identity); }); Bag(x =&gt; x.ReponsePointVerification, map =&gt; { map.Key( k =&gt; k.Column( "IdIntervention" ) ); }); } } </code></pre> <p>and </p> <pre><code>[Serializable] public class ReponsePointVerification { #region Public Properties public virtual int Id { get; set; } public virtual Intervention Intervention { get; set; } } public class ReponsePointVerificationMap : ClassMapping&lt;ReponsePointVerification&gt; { public ReponsePointVerificationMap() { Id&lt;int&gt;(x =&gt; x.Id, map =&gt; { map.Generator(NHibernate.Mapping.ByCode.Generators.Identity); }); ManyToOne&lt;Intervention&gt;(x =&gt; x.Intervention, map =&gt; map.Column("IdIntervention")); } } </code></pre> <p>which represents two tables Intervention and ReponsePointVerification which has a foreign key (IdIntervention) which is linked to Intervention.</p> <p>When I call :</p> <pre><code> return (from interv in Session.Query&lt;Intervention&gt;() .Fetch(rep =&gt; rep.ReponsePointVerification) select interv).ToList(); </code></pre> <p>or even when I don't fetch the information. I has this error :</p> <pre><code>Could not cast the value in field id0_ of type Int32 to the Type SerializableType. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type. </code></pre> <p>The sql query looks to be ok.</p> <p>I found that post <a href="http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc" rel="nofollow">http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc</a></p> <p>And I modify the InterventionMap class according to that post it will work :</p> <pre><code>public class InterventionMap : ClassMapping&lt;Intervention&gt; { public InterventionMap() { Id&lt;int&gt;(x =&gt; x.Id, map =&gt; { map.Generator(NHibernate.Mapping.ByCode.Generators.Identity); }); Bag(x =&gt; x.ReponsePointVerification, map =&gt; { map.Key( k =&gt; k.Column( "IdIntervention" ) ); }, rm =&gt; rm.OneToMany()); } } </code></pre> <p>The only difference is the add of ", rm => rm.OneToMany()"</p> <p>Trust me I'm very happy it worked but I really don't understand that line can someone explain me the meaning ?</p> <p>Regards</p> <p><strong>Edit</strong></p> <p>I made a WriteAllXmlMapping and the 2 codes are :</p> <p><strong>version without rm => rm.OneToMany() (which doesn't work)</strong></p> <pre><code>&lt;bag name="ReponsePointVerification"&gt; &lt;key column="IdIntervention" /&gt; &lt;element type="Hyma.BusinessObjets.ReponsePointVerification" /&gt; &lt;/bag&gt; </code></pre> <p><strong>version with rm => rm.OneToMany() (which work)</strong></p> <pre><code>&lt;bag name="ReponsePointVerification"&gt; &lt;key column="IdIntervention" /&gt; &lt;one-to-many class="ReponsePointVerification" /&gt; &lt;/bag&gt; </code></pre>
    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