Note that there are some explanatory texts on larger screens.

plurals
  1. POComposite Id's and Restrictions.IdEq or with comparison in Linq not working as expected
    primarykey
    data
    text
    <p>I have an entity where a composite id is used. I changed to code to make use of wrapping the composite id in a seperate key class. I expected that with Linq I could do a comparison on key object and with the Criteria API to use Restrictions.IdEq but both fail. I need to explicitly compare the key values to make it work.</p> <p>I cannot find any documentation if this should work so for the moment I am stuck with direct comparisons but this means that when I alter the key that I also need to update the query code which is obviously not what I would want.</p> <p>As a side note, I tried this with NHibernate 3.0.0 Alpha 2 and 3.</p> <h1>Domain</h1> <h3>Mapping</h3> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Cwc.Pulse.Dal" namespace="Cwc.Pulse.Dal"&gt; &lt;class name="AddonStatus"&gt; &lt;composite-id name="Id"&gt; &lt;key-many-to-one name="Context" column="Context_Id" class="Context" /&gt; &lt;key-property name="AddonType" column="Addon_Id"/&gt; &lt;/composite-id&gt; &lt;property name="Status" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <h3>Class</h3> <pre><code>public class AddonStatus { public virtual string Status { get; set; } public virtual Key Id { get; protected set; } public AddonStatus() { Id = new Key(); } public class Key { public virtual Context Context { get; set; } public virtual AddonType AddonType { get; set; } public override int GetHashCode() { return ContextId.GetHashCode() ^ AddonType.GetHashCode(); } public override bool Equals(object obj) { if (this == obj) return true; var o = obj as Key; if (null == o) return false; return Context == o.Context &amp;&amp; AddonType == o.AddonType; } } } </code></pre> <h1>Working queries</h1> <p>The queries below work and as you can see I compare the key values explicitly. I do not compare the key object.</p> <h3>Linq</h3> <pre><code>from status in session.Query&lt;AddonStatus&gt;() where status.Id.Context == context &amp;&amp; status.Id.AddonType == addonType select status </code></pre> <h3>Criteria API</h3> <pre><code>session.CreateCriteria&lt;AddonStatus&gt;() .Add(Restrictions.Eq("Id.Context", context)) .Add(Restrictions.Eq("Id.AddonType", addonType)) </code></pre> <h1>Expected to work but dont</h1> <p>I expect the following queries to work. Either in efficiently for linq in memory instead of the database but I expect the criteria api to be smart enough to handle such composite id´s in queries.</p> <p>Both linq and criteria api queries make use of a Key object comparison.</p> <pre><code>var key = new AddonStatus.Key { Context = context, AddonType = addonType }; </code></pre> <h3>Linq</h3> <pre><code>from status in session.Query&lt;AddonStatus&gt;() where status.Id == key select status </code></pre> <h3>Criteria API</h3> <pre><code>session.CreateCriteria&lt;AddonStatus&gt;() .Add(Restrictions.IdEq(key)) </code></pre> <p>So if anyone has such a scenario working then what am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    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