-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Why does 'find' return a list of Object ?
PostPosted: Tue Aug 16, 2005 3:28 am 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 4:10 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
Try to add the "select" statement (i.e. SELECT proc FROM ...) . I've seen some differences between Hibernate 2 & 3 in those query without the SELECT part, in Hib3 they do not work well as in Hib2, and most of the problems were solved adding the SELECT statement.
Hope this helps to workaround your problem.
Bye
Alessandro


Top
 Profile  
 
 Post subject: Solved
PostPosted: Tue Aug 16, 2005 4:23 am 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
arizzi wrote:
Try to add the "select" statement (i.e. SELECT proc FROM ...) . I've seen some differences between Hibernate 2 & 3 in those query without the SELECT part, in Hib3 they do not work well as in Hib2, and most of the problems were solved adding the SELECT statement.
Hope this helps to workaround your problem.
Bye
Alessandro


This actually solved the problem.
Thanks a lot. You have been given one credit for the answer.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 4:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
As per the hb docs, a query with a JOIN (rather than a FETCH JOIN) returns an array or results (ie. an Object[], not an Object).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.