Criteria.createCriteria(associationPath) not works for Key-many-to-one
----------------------------------------------------------------------
Key: HHH-2941
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2941
Project: Hibernate3
Issue Type: Bug
Components: query-criteria
Affects Versions: 3.2.4.sp1
Environment: Hibernate 3.2.4.sp1 + MySQL
Reporter: Eddie Man
Attachments: criteria_test.tar.gz
Suppose I had 2 class with the following content:
A.java
=================
public class A {
public A() {
}
public int ID;
public java.util.Set bs = new java.util.HashSet();
}
B.java
=================
import java.io.Serializable;
public class B implements Serializable {
public B() {
}
public int ID;
public A a;
}
A.hbm.xml
=================
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="A" table="A" lazy="false">
<id name="ID" column="ID" type="integer"
unsaved-value="0" access="field">
<generator class="native">
</generator>
</id>
<set name="bs" lazy="true" cascade="save-update,lock"
inverse="true" access="field">
<key column="AID" not-null="true"/>
<one-to-many class="B"/>
</set>
</class>
</hibernate-mapping>
B.hbm.xml
=================
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="B" table="B" lazy="false">
<composite-id>
<key-property name="ID" column="ID" type="integer"
access="field"/>
<key-many-to-one name="a" column="AID" class="A"
access="field">
</key-many-to-one>
</composite-id>
</class>
</hibernate-mapping>
Now, I want to find the B object with the specify B.ID and A.ID. Here is my code
Criteria criteria = session.createCriteria(B.class);
criteria.add(Expression.eq("ID", new Integer(2))); // B.ID = 2
criteria.createCriteria("a").add(Expression.eq("ID", new Integer(4)));
// A.ID = 4
B b = (B) criteria.uniqueResult();
I turned on the show_sql in my configure file so that it will prompt all the sql statement
in runtime, then executing the code, here is the printout:
09:35:59,096 WARN RootClass:211 - composite-id class does not override equals(): B
09:35:59,099 WARN RootClass:216 - composite-id class does not override hashCode(): B
Hibernate:
select
this_.ID as ID1_0_,
this_.AID as AID1_0_
from
B this_
where
this_.ID=?
and a1_.ID=?
09:35:59,982 WARN JDBCExceptionReporter:77 - SQL Error: 1054, SQLState: 42S22
09:35:59,982 ERROR JDBCExceptionReporter:78 - Unknown column 'a1_.ID' in
'where clause'
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could
not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1571)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
at ListTestData.main(ListTestData.java:17)
Caused by: java.sql.SQLException: Unknown column 'a1_.ID' in 'where
clause'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:930)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1024)
at
com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 7 more
It look like that the hibernate is missing the join table for the key-many-to-one. I had
tested to using <key-property> and <many-to-one insert="false"
update="false"> instread of <key-many-to-one>, and get no problem for
that, so is it the bug on key many to one?
The schema and test case is attached.
Steps of reproduce the problem with the attached project:
=========
1. Execute the schema.ddl in to mysql db.
2. Execute java application "InsertTestData"
3. Record the last inserted id from the database and modify the "ListTestData"
so that the expression value is correct.
4. Execute java application "ListTestData"
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira