• Post Reply Bookmark Topic Watch Topic
  • New Topic

createNativeQuery Question

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrien,

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

- Tariq
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!