Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I made the class LinkDAO to be Serializable . </p> <p>Not sure if you intend ObjectADAO and ObjectBDAO and to be like this:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3.Models { [Serializable] public class ObjectADAO { public virtual int Id { get; set; } public virtual string Name { get; set; } } [Serializable] public class ObjectBDAO { public virtual int Id { get; set; } public virtual string Name { get; set; } } } &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ConsoleApplication3" namespace="ConsoleApplication3.Models"&gt; &lt;class name="ObjectADAO"&gt; &lt;id name="Id" type="Int32" generator="native"/&gt; &lt;property name="Name" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ConsoleApplication3" namespace="ConsoleApplication3.Models"&gt; &lt;class name="ObjectBDAO"&gt; &lt;id name="Id" type="Int32" generator="native"/&gt; &lt;property name="Name" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>You seems to have some typos in your LinkDAO.hbm.xml . My version is as follow:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ConsoleApplication3" namespace="ConsoleApplication3.Models"&gt; &lt;class name="LinkDAO" table="LinkTable" lazy="false"&gt; &lt;composite-id&gt; &lt;key-many-to-one name="ObjectA" class="ObjectADAO" column="id_ObjectA"/&gt; &lt;key-many-to-one name="ObjectB" class="ObjectBDAO" column="id_ObjectB"/&gt; &lt;/composite-id&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The Test will will print correctly :</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleApplication3.Models; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly(typeof(LinkDAO).Assembly); new SchemaExport(cfg).Execute(script: true, export: true, justDrop: true); new SchemaExport(cfg).Execute(script:true, export:true, justDrop:false); var objAs = new ObjectADAO[] { new ObjectADAO { Name = "A1" }, new ObjectADAO { Name = "A2" } }; var objBs = new ObjectBDAO[] { new ObjectBDAO { Name = "B1" }, new ObjectBDAO { Name = "B2" } }; using (var _sessionFactory = cfg.BuildSessionFactory()) { using (var session = _sessionFactory.OpenSession()) { using (var tran = session.BeginTransaction()) { objAs.ToList().ForEach(x =&gt; session.Save(x)); objBs.ToList().ForEach(x =&gt; session.Save(x)); tran.Commit(); } using (var tran = session.BeginTransaction()) { session.Save(new LinkDAO { ObjectA = objAs[0], ObjectB = objBs[1] }); session.Save(new LinkDAO { ObjectA = objAs[1], ObjectB = objBs[0] }); tran.Commit(); } IList&lt;LinkDAO&gt; links = session.QueryOver&lt;LinkDAO&gt;().List(); links.All(lk =&gt; { Console.WriteLine("{0} {1}", lk.ObjectA.Name, lk.ObjectB.Name); return true; }); var ids = session.QueryOver&lt;LinkDAO&gt;().Select(x =&gt; x.ObjectA.Id, x =&gt; x.ObjectB.Id ).List&lt;object[]&gt;(); ids.All(ary =&gt; { Console.WriteLine("{0} {1}", ary[0], ary[1]); return true; }); } } } } } </code></pre> <p>At the end of the test run you will see :</p> <pre><code>NHibernate: SELECT this_.id_ObjectA as id1_2_0_, this_.id_ObjectB as id2_2_0_ FROM LinkTable this_ A1 B2 A2 B1 NHibernate: SELECT this_.id_ObjectA as y0_, this_.id_ObjectB as y1_ FROM LinkTable this_ 1 2 2 1 </code></pre>
 

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