Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch Tags and Tag count using HQL on SQL Server 2008
    primarykey
    data
    text
    <p>I'm implementing tagging on a particular entity, using NHibernate on SQL Server 2008. The structure I have now is, simplifying, like this:</p> <pre><code>public class Entity { public Guid Id { get; set; } } public class Tag { public Guid Id { get; set; } public string Name { get; set; } } public class TagAssoc { public Tag LinkedTag { get; set; } public Entity LinkedEntity { get; set; } //User //Other properties } </code></pre> <p>Nothing exotic: an entity can be tagged multiple times with the same tag, since the association also includes data about the user that tagged the entity and other stuff.</p> <p>Now, I'm trying to fetch a list of tags of a particular entity, with the counts of how many times the tag has been applied. Something like this in HQL:</p> <pre><code>select tass.LinkedTag, count(tass.LinkedTag) from TagAssoc as tass left outer join tass.LinkedTag as t group by tass.LinkedTag </code></pre> <p>This generates the following SQL query:</p> <pre><code>select tag1_.Id as Id0_, tag1_.Name as Name0_, tag1_.Id as x0_0_, count_big(tag1_.Id) as x1_0_ from BandTags tagassoc0_ left outer join Tags tag1_ on tagassoc0_.TagId=tag1_.Id group by tag1_.Id </code></pre> <p>This looks correct, but won't work in SQL Server 2008, because the Name property of Tag is not included in a "group by" clause. To make it work, I have to manually adjust the group by clause in order to include all properties of the Tag class:</p> <pre><code>select tass.LinkedTag, count(tass.LinkedTag) from TagAssoc as tass left outer join tass.LinkedTag as t group by tass.LinkedTag.Id, tass.LinkedTag.Name </code></pre> <p>But this depends on the properties of the Tag class and therefore would have to be updated every time the class is changed. Is there some other way to make the first HQL query work? Perhaps some HQL syntax that automatically makes the "group by" properties explicit?</p> <p>Thanks</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