Hello,
I am using joined-subclasses and want to use the count function on the super class to get the number of elements which are of the super class (but not of one of the subclasses).
The mapping:
Code:
<class name="News" table="news">
<cache usage="nonstrict-read-write" />
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<property name="name" />
<joined-subclass name="GameNews" table="game_news">
<key column="news" />
<many-to-one name="game" />
</joined-subclass>
<joined-subclass name="GameNews2" table="game_news2">
<key column="news" />
<many-to-one name="game" />
</joined-subclass>
</class>
The HQL query:
Code:
select count(*) my.package.News as this where this.class = my.package.News
The non-running SQL query:
Code:
select count(*) as x0_0_ from news news0_ where (case when news0__1_.news is not null then 1 when news0__2_.news is not null then 2 when news0_.id is not null then 0 end=0 )
Hibernate seems to generate wrong SQL: The query tries to access properties of the subclass-tables without generating a join.
It does work properly when querying objects:
HQL:
Code:
from my.package.News as this where this.class = my.package.News
running SQL:
Code:
select news0_.id as id, case when news0__1_.news is not null then 1 when news0__2_.news is not null then 2 when news0_.id is not null then 0 end as clazz_, news0__1_.game as game30_, news0__2_.game as game31_ from news news0_ left outer join game_news news0__1_ on news0_.id=news0__1_.news left outer join game_news2 news0__2_ on news0_.id=news0__2_.news where (case when news0__1_.news is not null then 1 when news0__2_.news is not null then 2 when news0_.id is not null then 0 end=0 )
Any help appreciated.
Thanks,
Till
Hibernate 2.1.1
MySQL 4.0.12