Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate Named query with table per subclass
    primarykey
    data
    text
    <p>I have a project where we are using only named queries to access the database. Recently we have created new entities that we were planing to map following the table per Subclass pattern, so we created the mappings following the documentation and everything seems good, except that we cannot find how to define the table aliases in the named queries in order to load each subclass.</p> <p>This is what we have done so far:</p> <p><strong>Entities:</strong></p> <pre><code>public class Gear { public virtual string Name { get; set; } public virtual string Slug { get; set; } } public class Pedal : Gear { public virtual PedalTypeEnum PedalType { get; set; } } </code></pre> <p><strong>Mappings:</strong></p> <pre><code>public class GearMap : ClassMap&lt;Gear&gt; { public GearMap() { Table("[Gear]"); Id(m =&gt; m.Id).Column("Id"); Map(m =&gt; m.Name).Column("[Name]"); Map(m =&gt; m.Slug).Column("[Slug]"); } } public class PedalMap : SubclassMap&lt;Pedal&gt; { public PedalMap() { Table("[Pedal]"); KeyColumn("[Gear_Id]"); Map(m =&gt; m.PedalType).Column("PedalType").CustomType(typeof(PedalTypeEnum)); } } </code></pre> <p>And the query that we are trying to execute is this one:</p> <pre><code>&lt;sql-query name="myquery" &gt; &lt;return alias="[Gear]" class="Gear" /&gt; &lt;![CDATA[ SELECT {[Gear].*}, FROM [Gear] {[Gear]} ]]&gt; &lt;/sql-query&gt; </code></pre> <p>And this results in the following SQL generated by nhibernate:</p> <pre><code>SELECT [Gear].[Id] as column1_12_0_, [Gear].[Name] as column3_12_0_, [Gear].[Slug] as column4_12_0_, [Gear]_1_.PedalType as PedalType13_0_, case when [Gear]_1_.[Gear_Id] is not null then 1 when [Gear].[Id] is not null then 0 end as clazz_0_ FROM [Gear] [Gear] </code></pre> <p>As you can see it detects the subclasses correctly and tries to load the field PedalType that only belongs to the subclass but as there is no table with the alias <code>[Gear]_1_.</code> the query fails...</p> <p>We have tried to make the join with the pedal table, but we are unable to find the right way to set the alias in a way that it translates to <code>[Gear]_1_.</code></p> <p>Any help?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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