Hello,
I'm using Hibernate 2.1.2/MySQL 3.26 for a Tibetan Dictionary application and now I am struggling with an HQL query that joins a collection but returns objects of class java.lang.Object that are of course not castable to any mapped subclass.
I'm trying to set constraints in the where clause based on values of objects in an indexed collection. I've solved all previous Hibernate problems with the FAQs, docs and forum searches but I'm stuck here. I'm concerned that it might be an issue with my mapping design. where the meta component is actually a property of the superclass of Term and Pronunciation, (LexComponent).
Here's my query:
Code:
from org.thdl.lex.component.Term as term
left join term.pronunciations as pronunciation
where term.meta.createdBy = 10
or pronunciation.meta.createdBy = 10
I can see in the Hibernate log that a Term and Pronunciation are correctly hydrated but I get a ClassCastException when I try to cast to ITerm or Term because the class is a plain old java.lang.Object. Any ideas would be greatly appreciated. If I remove the join (and the reference to it in where clause) the cast test passes.
The relevant portion of the mapping is:
Code:
<class name="org.thdl.lex.component.LexComponent" proxy="org.thdl.lex.component.ILexComponent" table="Meta">
<id name="metaId" type="java.lang.Integer" column="metaId"></id>
<joined-subclass name="org.thdl.lex.component.Term" proxy="org.thdl.lex.component.ITerm" table="Terms">
<key column="metaId"/>
<property name="term" type="java.lang.String" column="term" not-null="true" length="255"/>
<list name="pronunciations" table="Pronunciations" lazy="true">
<key column="parentId"/>
<index column="index"/>
<one-to-many class="org.thdl.lex.component.Pronunciation"/>
</list>
</joined-subclass>
<joined-subclass name="org.thdl.lex.component.Pronunciation" proxy="org.thdl.lex.component.IPronunciation" table="Pronunciations">
<key column="metaId"/>
<property name="parentId" type="java.lang.Integer" column="parentId" length="11"/>
<many-to-one name="parent" class="org.thdl.lex.component.LexComponent" column="parentId" insert="false" update="false"/>
<property name="phonetics" type="java.lang.String" column="phonetics" not-null="true" length="65535"/>
<property name="phoneticsType" type="java.lang.Integer" column="phoneticsType" not-null="true" length="6"/>
</joined-subclass>
<component name="meta" class="org.thdl.lex.component.Meta">
<property name="createdBy" type="java.lang.Integer" column="createdBy" not-null="true" length="11"/>
</component>
</class>
The java code is:
Code:
Query query = null;
Iterator it = null;
List terms;
String queryString = "from "
+ "org.thdl.lex.component.Term as term "
+ " left join term.pronunciations as pron "
+ " where term.meta.createdBy = :createdBy"
+ " or pron.meta.createdBy = :createdBy";
try
{
query = getSession().createQuery( queryString );
query.setInteger( "createdBy", 10 );
terms query.iterate();
terms=new LinkedList();
while (iterator.hasNext() )
{
terms.add( iterator.next() );
}
}
catch ( HibernateException he )
{
throw new LexRepositoryException( he );
}
return terms;
Here's the tail end of my unit test which includes the debug log of my seemingly succesful query. The assertion failed is just a class cast to ITerm. But casting to Term also fails
Code:
[java] 08:29:43,365 DEBUG Loader:215 - total objects hydrated: 2
[java] 08:29:43,380 DEBUG SessionImpl:2179 - resolving associations for [org.thdl.lex.component.Term#1]
[java] 08:29:43,416 DEBUG SessionImpl:2201 - done materializing entity [org.thdl.lex.component.Term#1]
[java] 08:29:43,432 DEBUG SessionImpl:2179 - resolving associations for [org.thdl.lex.component.Pronunciation#76]
[java] 08:29:43,434 DEBUG SessionImpl:1972 - loading [org.thdl.lex.component.LexComponent#1]
[java] 08:29:43,439 DEBUG SessionImpl:2068 - attempting to resolve [org.thdl.lex.component.LexComponent#1]
[java] 08:29:43,441 DEBUG SessionImpl:2083 - resolved object in session cache [org.thdl.lex.component.LexComponent#1]
[java] 08:29:43,446 DEBUG SessionImpl:2201 - done materializing entity [org.thdl.lex.component.Pronunciation#76]
[java] 08:29:43,456 DEBUG JDBCTransaction:73 - rollback
[java] 08:29:43,463 DEBUG SessionImpl:505 - transaction completion
[java] There are 1 terms matching your query.
[java] Object is of class name: [Ljava.lang.Object;
[java] F
[java] Time: 31.912
[java] There was 1 failure:
[java] 1) testFindTermsByMeta(org.thdl.lex.LexComponentRepositoryTest)junit.framework.AssertionFailedError
[java] at org.thdl.lex.LexComponentRepositoryTest.testFindTermsByMeta(LexComponentRepositoryTest.java:85)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at org.thdl.lex.TestAll.main(TestAll.java:22)
[java] FAILURES!!!
[java] Tests run: 1, Failures: 1, Errors: 0
Thanks so much for any insights here.
Sincerely,
Travis McCauley
Toronto, ON