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: Class Casting Exception with Hibernate Named Queries
PostPosted: Wed Jun 25, 2008 1:23 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

I tried with the hibernate named queries, it is working fine if i use one single table to retrive the data.

If i use more than table to compare and get the query result it is returning the general java.lang.object.

So, i tried casting the general to the specific class which i need but that object returns the combination of both the classes, so casting is not happening, so is there any to handle this situation.

Sample code which i tried,

event.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.gpj.gl.pa.domain.Event" table="O_EVENT_TX" schema="GL_PA">
        <id name="paEventId" type="string">
            <column name="PA_EVENT_ID" length="15" />

            <generator class="assigned" />
        </id>

        <many-to-one name="eventDb" class="com.gpj.gl.pa.domain.EventDb" fetch="select">
            <column name="GENERIC_EVENT_ID" length="15" not-null="true" />
        </many-to-one>

<query name="myEventSearchByCriteriaOR"><![CDATA[
       from EventDb as eventDB, Event as event
       where event.eventDb.genericEventId = eventDB.genericEventId and
          (event.eventDb.eventName = :eventName
           or event.eventDb.eventYear =:eventYear
             or event.eventDb.eventType.eventTypeId =:eventType
           or event.eventDb.countryCd =:eventCountry
           or event.eventDb.stateCd =:eventState
           or event.eventDb.eventCity =:eventCity)
     ]]></query>
</hibernate-mapping>


EventDaoImpl.java
Code:

   public List<Event> findSearchResult(String eventName, String eventYear, String eventType, String eventCountry, String eventState, String eventCity) {
      // TODO Auto-generated method stub
      List<Event> searchResult = null;
      String queryName = null;
      String[] argNames = null;
      Object[] args = null;
      
      if(!eventName.equals("") || !eventYear.equals("")
            || !eventType.equals("") || !eventCountry.equals("")
            || !eventState.equals("") || !eventCity.equals("")){

         queryName = "myEventSearchByCriteriaOR";
         argNames = new String[] { "eventName", "eventYear", "eventType",
               "eventCountry", "eventState", "eventCity" };
         args = new Object[] { eventName, eventYear,
               eventType, eventCountry, eventState, eventCity };

         searchResult = new ArrayList<Event>( getHibernateTemplate()
               .findByNamedQueryAndNamedParam(queryName, argNames, args));
         System.out.println("Generic Id DAO: "+ searchResult.size());
         
         System.out.println("Generic Id DAO: "+ searchResult.toString());
         
         for(int i=0; i<searchResult.size();i++){
            Event evt = (Event) searchResult.get(i);
            System.out.println("Generic Id DAO: "+ evt.getEventDb().getGenericEventId());
            System.out.println("PA ID "+evt.getPaEventId());   
         }
      }
      return searchResult;
   }
}


I would like to get the object of Event Class rather than general Object class or EventDb class.
below is the picture of searchResult object while debugging.
Quote:

searchResult ArrayList<E> (id=173)
elementData Object[2] (id=188)
[0] Object[2] (id=4955)
[0] EventDb (id=4965)
[1] Event (id=4964)
[1] Object[2] (id=4956)
[0] EventDb (id=4965)
[1] Event (id=4983)
modCount 0
size 2


Exception:
Quote:
Generic Id DAO: 2
Generic Id DAO: [[Ljava.lang.Object;@b3951d, [Ljava.lang.Object;@2cf63]
Jun 25, 2008 10:54:13 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet pa threw exception
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.gpj.gl.pa.domain.Event
at com.gpj.gl.pa.dao.impl.EventDaoImpl.findSearchResult(EventDaoImpl.java:75)
at com.gpj.gl.pa.service.impl.MyEventServiceImpl.findEventsByCreteria(MyEventServiceImpl.java:58)
at com.gpj.gl.pa.web.controller.SearchMyEventFormAction.myEventSearch(SearchMyEventFormAction.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)


Top
 Profile  
 
 Post subject: Your query is wrong
PostPosted: Wed Jun 25, 2008 4:19 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Change and replace your hql as follows:

from Event as event
where
event.eventDb.eventName = :eventName
or event.eventDb.eventYear =:eventYear
or event.eventDb.eventType.eventTypeId =:eventType
or event.eventDb.countryCd =:eventCountry
or event.eventDb.stateCd =:eventState
or event.eventDb.eventCity =:eventCity

Since Event contains EventDB and you want to retrieve object of event, so just write Event in from clause. In where condition, when you write event.eventDb.everntName, it automcatically makes a equijoin of Event with eventDB, so you don't need to do anything special.

_________________
amer sohail


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:18 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
amersohail794's advice should work

Sometimes it's interesting to do a .getClass() method on the object being returned, so you can actually see the true class being returned to your query. Sometimes it's more than just an Object, but perhaps, an instance of a different class in your domain model.

Just helpful little troubleshooting tip.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Class Casting Exception with Hibernate Named Queries
PostPosted: Fri Sep 11, 2009 12:47 pm 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
hi ambika,

I met the same problem. Adding <return> tag to <sql-query> colved my problem. Try adding this to your mapping file.
<return alias="event" class="com.gpj.gl.pa.domain.Event"/>

Shagufta


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.