Hibernate version: 3
The question :
In the samples below, I do two searches, one plain search, and one search with a simple join.
The first search is executed fine, and the correct subclass is instaciated and casted to one of it's implemented interfaces and returned.
The second search, which includes a join to make it possible to search via a property of a referenced class, does not instaciate the correct subclass, but instead returns instances of class Object. Of course, when I try to cast this to one of the implementing interfaces of the expected class, this fails.
Why does Hibernate create Object instead of the correct subclass when doing the second query ?
Mapping documents:Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="no.song.b2b.dao.OrderDataDao" table="ordre" lazy="false" >
<id name="ordreNummer" column="ordre_data_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="data_type" type="string"/>
<property name="orderData" column="ordre_data" type="text"/>
<subclass name="no.song.b2b.domain.OrderDataImpl" discriminator-value="generic" lazy="false"/>
<subclass name="no.song.b2b.clients.jara.JaraOrderDataImpl" discriminator-value="jara" lazy="false" />
</class>
<class name="no.song.b2b.dao.OrderProcessDao" table="ordre_prosess">
<id name="processId" column="prosess_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="prosess_type" type="string"/>
<many-to-one name="orderData"
class="no.song.b2b.dao.OrderDataDao"
cascade="all"
column="ordre_data"
unique="true" />
<subclass name="no.song.b2b.dao.JaraOrderProcessDao">
<join table="ordre_prosess_jara">
<key column="prosess_id"/>
</join>
<subclass name="no.song.b2b.clients.jara.JaraOrderProcessImpl" discriminator-value="Jara_1">
<set name="transactions" lazy="true" inverse="true" cascade="all">
<key column="prosess_id"/>
<one-to-many class="no.song.b2b.dao.JaraTransactionDAO" />
</set>
</subclass>
</subclass>
</class>
<class name="no.song.b2b.dao.JaraTransactionDAO" table="jara_transaksjoner">
<id name="transactionId" column="transaksjon_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="transaksjons_type" type="string"/>
<property name="conversationId" column="samtale_id"/>
<property name="stateMessage" column="status_melding"/>
<property name="state" column="status_kode"/>
<property name="executionOrder" column="exec_order"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetCustomerByNumber" discriminator-value="customerByNumber"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetCustomerBySearch" discriminator-value="customerBySearch"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetAddressBySearch" discriminator-value="addressBySearch"/>
</class>
</hibernate-mapping>
Spring-hibernate-search-methods :
Code:
public JaraTransaction findTransactionByConversationId(String convId){
List l = getHibernateTemplate().find("from JaraTransactionDAO t where t.conversationId = ? ",new Object[]{convId});
log.debug("no joins :"+l.get(0).getClass().getName());
return (JaraTransaction) l.get(0);
}
public JaraOrderProcess findJaraProcessByTransactionId(Integer transId) {
List l = getHibernateTemplate().find("from JaraOrderProcessDao as proc join proc.transactions as trans where trans.transactionId = ?",transId);
if (l.size() > 0) {
log.debug("with join: "+l.get(0).getClass().getName());
return (JaraOrderProcess) l.get(0);
}
return null;
}
the test-code :
Code:
log.debug("------trans by conv----");
trans = dao.findTransactionByConversationId("-k1JHC8QiR4hrUjtXWzzw3SUzzw");
log.debug("------by id----");
proc = dao.findJaraProcessByTransactionId(new Integer(10));
the log :
Quote:
2005-08-16 09:11:37,908 DEBUG [test.testComponent] - ------trans by conv----
2005-08-16 09:11:38,339 DEBUG [org.hibernate.SQL] - select jaratransa0_.transaksjon_id as transaks1_, jaratransa0_.samtale_id as samtale3_3_, jaratransa0_.status_melding as status4_3_, jaratransa0_.status_kode as status5_3_, jaratransa0_.exec_order as exec6_3_, jaratransa0_.transaksjons_type as transaks2_ from jara_transaksjoner jaratransa0_ where jaratransa0_.samtale_id=?
2005-08-16 09:11:38,349 DEBUG [no.song.b2b.dao.B2bDao] - no joins :no.song.b2b.clients.jara.transactions.GetCustomerByNumber
2005-08-16 09:11:38,349 DEBUG [test.testComponent] - ------by id----
2005-08-16 09:11:38,359 DEBUG [org.hibernate.SQL] - select jaraorderp0_.prosess_id as prosess1_0_, transactio1_.transaksjon_id as transaks1_1_, jaraorderp0_.ordre_data as ordre3_1_0_, jaraorderp0_.prosess_type as prosess2_0_, transactio1_.samtale_id as samtale3_3_1_, transactio1_.status_melding as status4_3_1_, transactio1_.status_kode as status5_3_1_, transactio1_.exec_order as exec6_3_1_, transactio1_.transaksjons_type as transaks2_1_ from ordre_prosess jaraorderp0_ inner join ordre_prosess_jara jaraorderp0_1_ on jaraorderp0_.prosess_id=jaraorderp0_1_.prosess_id inner join jara_transaksjoner transactio1_ on jaraorderp0_.prosess_id=transactio1_.prosess_id where jaraorderp0_.prosess_type='Jara_1' and transactio1_.transaksjon_id=?
2005-08-16 09:11:38,359 DEBUG [org.hibernate.SQL] - select orderdatad0_.ordre_data_id as ordre1_0_, orderdatad0_.ordre_data as ordre3_0_0_, orderdatad0_.data_type as data2_0_ from ordre orderdatad0_ where orderdatad0_.ordre_data_id=?
2005-08-16 09:11:38,379 DEBUG [org.hibernate.SQL] - select transactio0_.prosess_id as prosess7_1_, transactio0_.transaksjon_id as transaks1_1_, transactio0_.transaksjon_id as transaks1_0_, transactio0_.samtale_id as samtale3_3_0_, transactio0_.status_melding as status4_3_0_, transactio0_.status_kode as status5_3_0_, transactio0_.exec_order as exec6_3_0_, transactio0_.transaksjons_type as transaks2_0_ from jara_transaksjoner transactio0_ where transactio0_.prosess_id=?
2005-08-16 09:11:38,399 DEBUG [org.hibernate.SQL] - update jara_transaksjoner set prosess_id=null where prosess_id=?
2005-08-16 09:11:38,399 DEBUG [org.hibernate.SQL] - update jara_transaksjoner set prosess_id=? where transaksjon_id=?
2005-08-16 09:11:38,399 DEBUG [org.hibernate.SQL] - update jara_transaksjoner set prosess_id=? where transaksjon_id=?
2005-08-16 09:11:38,399 DEBUG [no.song.b2b.dao.B2bDao] - with join: [Ljava.lang.Object;
Process finished with exit code -1
java.lang.ClassCastException
at no.song.b2b.dao.B2bDao.findJaraProcessByTransactionId(B2bDao.java:48)
at test.testComponent.testAll(testComponent.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)