Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interestingly, I'm getting almost the exact opposite of this behavior in 2.1.2.</p> <h3>My mapping (simplified):</h3> <pre><code>&lt;!-- Subscriber class --&gt; &lt;class name="Subscriber" &gt; &lt;composite-id name="SubscriberKey" class="SubscriberKey"&gt; &lt;key-property name="Request" column="RequestID" type="int"/&gt; &lt;key-many-to-one name="User" column="UserID" class="User" not-found="ignore" /&gt; &lt;/composite-id&gt; &lt;!-- User class - note that this goes to a different schema, and is not mutable. Who knows if that's important... --&gt; &lt;class name="User" schema="AnotherDb.dbo" mutable="false"&gt; &lt;id name="Id" column="UserID" type="int"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="FirstName" column="FirstName" type="string" /&gt; &lt;property name="LastName" column="LastName" type="string" /&gt; </code></pre> <h3>goes to:</h3> <pre><code>public class User { public virtual int? Id {get; protected set;} public virtual string FirstName { get; protected set; } public virtual string LastName { get; protected set; } public User() { } } public class Subscriber { public virtual SubscriberKey SubscriberKey { get; set; } public virtual User User { get; set; } public Subscriber() { } } public class SubscriberKey { public override bool Equals(object obj) { if (obj is SubscriberKey &amp;&amp; obj != null) return ((SubscriberKey)obj).Request == Request &amp;&amp; ((SubscriberKey)obj).User.Id == User.Id; return false; } public override int GetHashCode() { return (Request.ToString() + User.Id.ToString()).GetHashCode(); } public virtual int Request { get; set; } public virtual User User { get; set; } public SubscriberKey() { } } </code></pre> <h3>Things which work:</h3> <pre><code>CreateCriteria&lt;Subscriber&gt;() .Add(Restrictions.IdEq(keyInstance)) .UniqueResult&lt;Subscriber&gt;(); CreateCriteria&lt;Subscriber&gt;() .Add(Restrictions.Eq("SubscriberKey.User.Id", aUserID)) .Add(Restrictions.Eq("SubscriberKey.Request", aRequestID)) .UniqueResult&lt;Subscriber&gt;(); </code></pre> <h3>Things which don't work:</h3> <pre><code>Get&lt;Subscriber&gt;(keyInstance); </code></pre> <p>I'm thinking this is an inconsistency between their various ID-equaling query forms. When I get time, I'll be building a minimal unit test to submit as a bug example. I'd be interested in any / all thoughts anyone might have on this...</p> <hr> <p>edit: Heeey, I figured it out!</p> <h2>Things which do work, now that I've read <a href="http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx" rel="nofollow">this</a></h2> <pre><code>Get&lt;Subscriber&gt;(new SubscriberKey() { User = Load&lt;User&gt;(aUserID), // the important part! Request = aRequestID }); </code></pre> <p>This will create a proxy object for the User key, without hitting the database (unless necessary). If you swap <code>Load&lt;User&gt;</code> for <code>Get&lt;User&gt;</code>, you'll immediately hit the database to populate the object, rather than respecting your lazy-loading properties. Use Load.</p> <p>And things like this are precisely why people suggest the (type)Repository pattern - I can do this behind the scenes: <code>Get&lt;&gt;(new SK(){User=Load&lt;&gt;(key.User.Id)}</code>, and still <code>Get(key)</code> by a single key, identical to every other object.</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.
    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