-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to get the ResultSetMetaData for SQLQuery.list()?
PostPosted: Thu Aug 04, 2011 10:25 pm 
Newbie

Joined: Mon Jul 18, 2011 2:35 pm
Posts: 1
Hi,

We are on Hibernate core 3.3.1.GA. A requirement is forcing us to do a straight SQL query. Part of it specifies that we need to return the column names along with the values.

Looking through the API, I don't see a clear way to get at the metadata.

So, for now we did a straight JDBC query, but as you can imagine that is a hassle to escape the parameters.

For reference we ended up doing something like this:

Code:
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();
            int columnStartIndex = 1;
            int columnStopIndex = columnCount - 1;

            while (resultSet.next()) {
                Map<String, Object> result = new HashMap<String, Object>(columnCount);
                for (int i = columnStartIndex; i < columnStopIndex; i++) {
                    result.put(resultSetMetaData.getColumnName(i), resultSet.getObject(i));
                }
                results.add(result);


Best wishes,

Grant


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.