• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

createNativeQuery Question

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adrien,

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

- Tariq
 
reply
    Bookmark Topic Watch Topic
  • New Topic