| 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
|
 |
 |
|
|
subject: createNativeQuery Question
|
|
|