Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raul Vila
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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!
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Finally yes, my working solution is:



Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic