Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate 1.2 with SubclassMap causes "No row with the given identifier" error
    primarykey
    data
    text
    <p>I am trying to upgrade to Fluent NHibernate 2.1 (Build #694). As a result, I am also upgrading to NHibernate 3.0. I am having an issue with a "Table-Per-Subclass" mapping, which results in error when trying to retrieve data. </p> <p><strong>Important Note:</strong> these tables and classes worked with the now deprecated version of "Joined-Subclass" mapping which existed in a previous version of FluentNhibernate, which allowed the subclass to have its own unique id.</p> <p>I have whittled the code down to its smallest parts, so let me explain, via code and it will become more clear:</p> <p>Here are the tables involved:</p> <p><img src="https://i.stack.imgur.com/powFU.png" alt="Tables"></p> <p>Here are the classes representing the tables:</p> <pre><code>public class Field { public virtual int Id { get; set; } public virtual string Code { get; set; } public virtual string Description { get; set; } } public class MenuItem : Field { public virtual string NavigateUrl { get; set; } } public class UserLink { public virtual int Id { get; set; } public virtual string ExternalLinkName { get; set; } public virtual MenuItem MenuItem { get; set; } public virtual int UserId { get; set; } } </code></pre> <p>Here are the corresponding Mappings:</p> <pre><code>public class FieldMap : ClassMap&lt;Field&gt; { public FieldMap() { Table("Field"); Id(x =&gt; x.Id, "ID").GeneratedBy.Identity(); Map(x =&gt; x.Code, "Code"); Map(x =&gt; x.Description, "Description"); } } public class MenuItemMap : SubclassMap&lt;MenuItem&gt; { public MenuItemMap() { Table("MenuItem"); Map(x =&gt; x.NavigateUrl, "NavigateUrl"); } } public class UserLinkMap : ClassMap&lt;UserLink&gt; { public UserLinkMap() { Table("UserLink"); Id(x =&gt; x.Id, "ID").GeneratedBy.Identity(); Map(x =&gt; x.ExternalLinkName, "ExternalLinkName"); Map(x =&gt; x.UserId, "User_ID"); References(x =&gt; x.MenuItem).Column("ID"); } } </code></pre> <p>Here is the test:</p> <pre><code>[Test] public void CanRetrieveUserLinks() { ISession session = GetSession(); DetachedCriteria criteria = DetachedCriteria.For(typeof (UserLink)) .Add(Restrictions.Eq("UserId", 1)); ICriteria executableCriteria = criteria.GetExecutableCriteria(session); var userLinks = executableCriteria.List&lt;UserLink&gt;(); Assert.IsFalse(string.IsNullOrEmpty(userLinks[0].MenuItem.NavigateUrl)); session.Close(); } </code></pre> <p>When the Assert line is executed, the SQL generated is incorrect as it <strong>tries to lookup the MenuItem by Field_ID instead of ID</strong>. Therefore, I receive the error: <code>NHibernate.ObjectNotFoundException: No row with the given identifier exists[AS.AIMS.DomainModel.MenuItem#11]</code></p> <p>First the sql is generated to retrieve the userLinks, which is correct:</p> <pre><code>SELECT this_.ID as ID2_0_, this_.ExternalLinkName as External2_2_0_, this_.User_ID as User3_2_0_ FROM UserLink this_ WHERE this_.User_ID = 1 /* @p0 */ </code></pre> <p>Then to retrieve the Menu Item, it uses Field_Id instead of ID:</p> <pre><code>SELECT menuitem0_.Field_id as ID0_0_, menuitem0_1_.Code as Code0_0_, menuitem0_1_.Description as Descript3_0_0_, menuitem0_.NavigateUrl as Navigate2_1_0_ FROM MenuItem menuitem0_ inner join Field menuitem0_1_ on menuitem0_.Field_id = menuitem0_1_.ID WHERE menuitem0_.Field_id = 11 /* @p0 */ </code></pre>
    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