-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query Metadata
PostPosted: Tue Mar 15, 2005 11:18 am 
Newbie

Joined: Wed Nov 17, 2004 10:53 am
Posts: 3
Hibernate version: 3.0

Hi,

I'd like to know if there's a way to access the property name of the returned results for an hql query. For example, for this query : "select client.firstName, client.lastName from Client client", i'd like to obtain metadata information that would give me access to the two property names : "firstName" and "lastName".

I found a way to access the return types ( Query.getReturnTypes() ), but not the return names.

On a JDBC ResultSet, the getResultSetMetaData() returned a structure that allowed me to get the column names. An equivalent structure could be available in an hql query via a getQueryMetadata() method.

This feature would be quite useful for the following purposes :
- Tools like the Hibernate Console could show the returned property names for an hql query.
- We could access the returned values of an hql query by name rather than by position in an Object array. For a complex select with many returned properties, this would be really useful.
- I use a generic tool to serialize the result of a query in an xml structure. I would like to use the name of the properties as the xml element names.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 7:00 pm 
Newbie

Joined: Wed Mar 30, 2005 6:50 pm
Posts: 8
I am having a very similar problem. I am trying to fit the queries into a generic JSF DataModel so that the UIData control can reference the data by column names rather than by column index. If I could get the column names I would be able to create a suitable mapping.

Apart from parsing the query string itself, I can't figure out a way to get the column names. Has anyone else tried doing this before?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 11:04 pm 
Newbie

Joined: Wed Mar 30, 2005 6:50 pm
Posts: 8
I found a workaround that enabled to get me the parameter names from a named SQL query.

Session session = HibernateUtil.currentSession();
SessionFactoryImpl sessImpl = (SessionFactoryImpl) session.getSessionFactory();

NamedSQLQueryDefinition nsqlqd = sessImpl.getNamedSQLQuery( NamedQueryString );
// get the column definitons
SQLQueryScalarReturn[] columnDefinitions = nsqlqd.getScalarQueryReturns();

// get the query using the Plumtree Query Impl Helper class
Query query = PTSQLQueryImplHelper.getSQLQueryImpl(nsqlqd, (SessionImpl)session);
query.setCacheable( nsqlqd.isCacheable() );
query.setCacheRegion( nsqlqd.getCacheRegion() );
if ( nsqlqd.getTimeout()!=null ) query.setTimeout( nsqlqd.getTimeout().intValue() );
if ( nsqlqd.getFetchSize()!=null ) query.setFetchSize( nsqlqd.getFetchSize().intValue() );
List l = query.list();

The only strange part is that you need to create a helper class or a subclass that essentially creates a SQLQuery object. Since SQLQuery has a default constructor you don't have access to the class unless you are in the same package. So in the previous example my PTSQLQueryHelper class exists in org.hibernate.impl.

I am also sure you can do the same for non-named queries, although my example does not demostrate this. Cheers

Dan


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

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.