Is it possible to write a HQL query that groups results by the discriminator value of a table per class hierarchy mapping? For instance
"select discriminator d, count(*) c from Foo group by discriminator"
with a mapping like
<hibernate-mapping>
<class abstract="true" name="Foo">
<!-- SNIP -->
<subclass name="Bar" discriminator-value="BAR">
<!-- SNIP -->
</subclass>
<subclass name="Baz" discriminator-value="BAZ">
<!-- SNIP -->
</subclass>
</class>
</hibernate-mapping>
and a possible result like
+-----+---+
| d | c |
+-----+---+
| BAR | 3 |
| BAZ | 4 |
+-----+---|
So what I’m looking for is a valid replacment for discriminator in my HQL query. Is there such as thing or do I have to go for raw SQL?
The
classattribute does that: from the Hibernate docApparently it is necessary to use aliases to refer to your entities when using the class attribute, at least in the version of Hibernate I’m using.
So your HQL request should be:
And this will return arrays containing "BAR" and "BAZ" along with their respective counts.