".class" with <joined-subclass> problem

Description

I've acknowledged an error that happens using "select count ..."
together to a where clause "... where alias.class = ClassName ".

Suppose to have two classes:

public class Foo
{
String id;
String attr1;

public String getId(){ return id; }
public void setId(String id){ this.id = id; }
public String getAttr1(){ return attr1; }
public void setAttr1(String attr1){ this.attr1 = attr1; }
}

public class Bar extends Foo
{
String attr2;

public String getAttr2(){ return attr2; }
public void setAttr2(String attr2){ this.attr2 = attr2; }
}

and the corresponding mapping file:

<hibernate-mapping>
<class name="test.persistents.Foo" table="Foo">
<id name="id" type="string">
<column name="id" length="32"/>
<generator class="uuid.hex"/>
</id>
<property name="attr1" column="attr1" type="string"/>

<joined-subclass name="test.persistents.Bar" table="Bar">
<key column="foo_id"/>
<property name="attr2" column="attr2" type="string"/>
</joined-subclass>
</class>
</hibernate-mapping>

When I try to make the following query:

Query query = session.createQuery("select count from Foo foo where foo.class = Foo");

I get an error of this type:

java.sql.SQLException: General error, message from server: "Unknown table 'foo0__1' in where clause"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1874)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1538)
at com.p6spy.engine.logging.P6LogPreparedStatement.executeQuery(P6LogPreparedStatement.java:171)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:140)
at net.sf.hibernate.loader.Loader.find(Loader.java:620)
at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:76)
at test.Test.main(Test.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)

while if I perform the same query without the select count (select foo from Foo foo where foo.class = Foo)
all works fine.
Comparing the two queries produced by hibernate in both cases,
it seems that the error derives from the lack of the "left outer join" with the Bar table in the "from" clause:

with select count:
select count as x0_0_
from Foo foo0_
where (case when foo0_1.foo_id is not null then 1 when foo0.id is not null then 0 end=0 )

without select count:
select foo0_.id as id,
case when foo0__1.foo_id is not null then 1
when foo0_.id is not null then 0 end
as clazz_,
foo0_.attr1 as attr10_,
foo0_1.attr2 as attr21
from Foo foo0_ left outer join Bar foo0_1 on foo0.id=foo0__1.foo_id
where (case when foo0_1.foo_id is not null then 1 when foo0.id is not null then 0 end=0 )

in fact the query should be the following:
select count as x0_0_
from Foo foo0_ left outer join Bar foo0_1 on foo0.id=foo0__1.foo_id
where (case when foo0_1.foo_id is not null then 1 when foo0.id is not null then 0 end=0 )

Environment

windows 2000

Assignee

Unassigned

Reporter

Simone Ricciardi

Labels

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Priority

Minor
Configure