Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get NHibernate to return data on composite key table?
    primarykey
    data
    text
    <p>I have a junction table in my database. I need to read the id values and use them in my program. I cannot get NHibernate to return any records in the database. It appears as though my mapping file "compiles", but no results are returned.</p> <p><strong>LinkTable</strong></p> <ul> <li><strong>int</strong> id_ObjectA <em>(foreign key)</em></li> <li><strong>int</strong> id_ObjectB <em>(foreign key)</em></li> </ul> <p><strong>LinkDAO Class:</strong></p> <pre><code>public class LinkDAO { public virtual ObjectADAO ObjectA { get; set; } public virtual ObjectBDAO ObjectB { get; set; } public override bool Equals(object obj) { if (obj == null) { return false; } var t = obj as LinkDAO; if (t == null) { return false; } return (t.ObjectA == ObjectA &amp;&amp; t.ObjectB == ObjectB); } public override int GetHashCode() { return (ObjectA.Name + "|" + ObjectB.Name).GetHashCode(); } } </code></pre> <p><em><strong>Mapping File</strong> 'LinkDAO.hbm.xml':</em></p> <pre><code>&lt;class name="LinkDAO" table="LinkTable" lazy="false"&gt; &lt;composite-id&gt; &lt;key-property name="ObjectA" column="id_ObjectA" /&gt; &lt;key-property name="ObjectB" column="id_ObjectB" /&gt; &lt;/composite-id&gt; &lt;/class&gt; </code></pre> <p>I'm querying like this:</p> <pre><code>IList&lt;LinkDAO&gt; links = NHibernateContext.Current().Session.QueryOver&lt;LinkDAO&gt;().List(); //Empty list is returned, but I can check the DB and see records </code></pre> <p><strong>Question:</strong> How do I read the two columns as int values when the table is comprised of only a composite key? (Having references to the actual object would be useful, but the int ids are sufficient)</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.
    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