aspose file tools*
The moose likes Object Relational Mapping and the fly likes [JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)?" Watch "[JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)?" New topic
Author

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

Raul Vila
Greenhorn

Joined: Jan 17, 2012
Posts: 14
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

Joined: Oct 01, 2007
Posts: 553
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"

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Raul Vila
Greenhorn

Joined: Jan 17, 2012
Posts: 14
(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

Joined: Jan 17, 2012
Posts: 14
Finally yes, my working solution is:



Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)?