通过每个类层次结构映射的表中的鉴别器值进行分组 [英] Group by discriminator value in table per class hierarchy mapping
问题描述
是否可以编写一个HQL查询,按照每个类层次结构映射的表的鉴别器值对结果进行分组?例如
选择鉴别器d,通过鉴别器从Foo组中计数(*)c
属性做到这一点:从Hibernate doc中获得
$ c
映射类似于
< / p> ;冬眠映射>
< class abstract =truename =Foo>
<! - SNIP - >
< subclass name =Bardiscriminator-value =BAR>
<! - SNIP - >
< / subclass>
< subclass name =Bazdiscriminator-value =BAZ>
<! - SNIP - >
< / subclass>
< / class>
< / hibernate-mapping>
以及可能的结果,例如
+ ----- + --- +
| d | c |
+ ----- + --- +
| BAR | 3 |
| BAZ | 4 |
+ ----- + --- |
所以我在找的是
discriminator
在我的HQL查询中。有没有这样的事情,或者我必须去原始的SQL?
lockquote
特殊属性类访问实例的鉴别器值多态持久性。嵌入在where子句中的Java类名将被转换为其鉴别值。
来自Cat cat where cat.class = DomesticCat
显然,在使用class属性时,必须使用别名来引用实体,至少在我使用的Hibernate版本中。
所以你的HQL请求应该是:
pre $ $ $ $ f.class
这将返回包含BAR和BAZ的数组以及它们各自的计数。
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 class
attribute does that: from the Hibernate doc
The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
from Cat cat where cat.class = DomesticCat
Apparently 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:
select f.class, count(*) c from Foo f group by f.class
And this will return arrays containing "BAR" and "BAZ" along with their respective counts.
这篇关于通过每个类层次结构映射的表中的鉴别器值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!