• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

[JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have searched in the forums and I haven't found anything...

The need is to execute a user defined query (so I don't know anything about it) and read the column names.
I didn't find how to get de column name of a List<> and I didn't find how to parse the CreativeNativeQuery.getResultList to a ResultSet (when I trid I get: java.util.Vector cannot be cast to java.sql.ResultSet).

The only solution would be read them from the query, but it won't work with "*" parameter.

Thanks for the help!
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getResultList() returns a List of results, for a native query this will most likely be an List of Object[], where the Object[] contains each piece of data you selected.
If you native query used a resultClass, then it can return a object.
You cannot get a JDBC ResultSet using JPA, if you want a JDBC ResultSet, then just use JDBC.

There is no way to get the column names, only the data is returned, you are assumed to know what you selected.

If you are using EclipseLink you can set a hint of the native query to return a List of Map instead of List of Object[] so you get the column names.

"eclipselink.result-type"="Map"
 
Raul Vila
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(First of all, Thanks)

You cannot get a JDBC ResultSet using JPA, if you want a JDBC ResultSet, then just use JDBC.


It sound logical, the only problem I see (it doesn't mean there is a problem) is to duplicate the connection data.

I have found this, but I cannot test it:

(I am using EJB 3, JPA 2... I have found different solutions for JPA1 and Hibernate).

I will post the results when I try.
 
Raul Vila
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally yes, my working solution is:



Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic