Hi there,
it seems that Hibernate generates a wrong SQL query (constructed from an HQL query) when I refer to a property of a mapped base class.
My HQL query is this:
Code:
from Client c where 1=1 and lower(c.person.email.email) like ?
The SQL displayed by Hibernate via show_sql is this:
Code:
Hibernate: select client0_.id as id, client0_.party_ref as party_ref from bo_Client client0_, bo_Person person1_, bo_Email email2_ where (1=1 )and(lower(email2_.email)like ? and client0_.party_ref=person1_.id and person1__1_.email_ref=email2_.id) limit ?
As you can see, this query refers to "person1__1_" that wasn't defined before. Additionally the query should include the bo_Party table I believe (see mappings below).
Is there a workaround for this?
Thanks,
Oliver
Hibernate version: 2.1.7c
Mapping documents:(excerpt)
Code:
<hibernate-mapping default-cascade="save-update">
<class name="Party" table="bo_Party">
<id name="id" type="int" column="id" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="email" column="email_ref" class="Email" />
<!-- more properties and associations ... -->
<joined-subclass name="Person" table="bo_Person">
<key column="id" />
<!-- more properties and associations ... -->
</joined-subclass>
<joined-subclass name="Company" table="bo_Company">
<key column="id" />
<!-- more properties and associations ... -->
</joined-subclass>
</class>
<class name="Email" table="bo_Email" lazy="true">
<id name="id" type="int" column="id" unsaved-value="0" >
<generator class="native"/>
</id>
<property name="email" column="email" type="string"/>
<!-- more properties ... -->
</class>
<class name="Client" table="bo_Client" lazy="true">
<id name="id" type="int" column="id" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="person" column="party_ref" class="Person" not-null="true"/>
<!-- more associations ... -->
</class>
<!-- more classes ... -->
Code between sessionFactory.openSession() and session.close():I'm using the Spring framework. The relevant DAO lines are simply these:
Code:
// dynamically create a StringBuffer containing the HQL [not shown]
Query query = getHibernateTemplate().createQuery(session, sb.toString());
// set parameters
int paramCount = params.size();
for (int i=0; i<paramCount; i++)
query.setString(i, (String)params.get(i));
query.setFirstResult(firstIndex)
.setMaxResults(maxResults);
return query.list();
Full stack trace of any exception that occurs:
(excerpt):
ERROR com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:80) - org.springframework.jdbc.UncategorizedSQLException: (Hibernate operation): encountered SQLException [General error, message from server: "Unknown table 'person1__1_' in where clause"]; nested exception is java.sql.SQLException: General error, message from server: "Unknown table 'person1__1_' in where clause"
javax.faces.el.EvaluationException: org.springframework.jdbc.UncategorizedSQLException: (Hibernate operation): encountered SQLException [General error, message from server: "Unknown table 'person1__1_' in where clause"]; nested exception is java.sql.SQLException: General error, message from server: "Unknown table 'person1__1_' in where clause"
[...]
Caused by: java.sql.SQLException: General error, message from server: "Unknown table 'person1__1_' in where clause"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1555)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:205)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
Name and version of the database you are using:
MySQL 4.0.18
The generated SQL (show_sql=true):
see above