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)