This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes createNativeQuery Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "createNativeQuery Question" Watch "createNativeQuery Question" New topic
Author

createNativeQuery Question

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Hi,

I am trying to use createNativeQuery to get certain column values which would be set to corresponding variables. Here's what I have in the code for this -

...

...

My problem is the query is an UNION of two tables as inline views and cannot typecast the List as one of the entity object. I tried to do something like -

...


But was getting ClassCastException probably as the datatype of table1 class did not match with the queryresults corresponding list value. I am guessing the queryresults value were being mapped incorrectly. But again I could be wrong.
Anyway, Is there any better way to extract these 4 column values from the list into separate variables?

Thanks


Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 72
Hi Tariq,

do you have try to check the object type in your .getResultList() ?
List<Object> results = em.createNativeQuery(sqlStmt).getResultList();

for(Object obj: results){
//check the type of entity
System.out.println("entity: "+obj.getClass());
if(obj instanceof entityTable1){
//cast in entity or do something
}
if(obj instanceof entityTable2){
//cast in entity or do something
}
}





For separate variables :

Or you can subList you results try subList(beginIndex,endingIndex);
Or toArray() ?


Good day


SCJP 5, SCDJWS 5
http://adrien-ruffie.blogspot.com
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Adrien,

Thanks for your prompt reply. I'll try those two option out.

- Tariq
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: createNativeQuery Question
 
Similar Threads
Execute pl/sql function which is having bind variables in java
Simple sql
PreparedStatement executeUpdate()
Question of using SqlResultSetMapping
2D Array or linked list