-->
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.  [ 2 posts ] 
Author Message
 Post subject: get aliases when running sql queries
PostPosted: Thu Jun 16, 2011 10:21 am 
Newbie

Joined: Tue Mar 03, 2009 9:32 am
Posts: 7
Hi,

I'm using hibernate (3.3) to work against a legacy schema.
I am running sql queries like this, for example:

Code:
Query query = getSession().createSQLQuery(
        "select st.description \"The Description\", st.value \"The Value\" " +
        "from some_table st");
   List list = query.list();


Don't ask why (this is a legacy schema), but I can't create an hibernate entity for the expected result, so the list I get contains Objects with 2 nameless properties.

My question is: how can I get the property names, a.k.a the aliases ("The Description" and "The Value")? With JDBC I could just dig it from the resultset/statement...

Is there an easy way to do that in hibernate?

Your help is much appreciated

_________________
Regards,
Uri Bar


Top
 Profile  
 
 Post subject: Re: get aliases when running sql queries
PostPosted: Thu Jun 16, 2011 10:31 am 
Newbie

Joined: Tue Mar 03, 2009 9:32 am
Posts: 7
Well, that was fast... :)

Here's how you can do it:
Add
Code:
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

So the code will become:

Code:
Query query = getSession().createSQLQuery(
        "select st.description \"The Description\", st.value \"The Value\" " +
        "from some_table st");
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = query.list();

_________________
Regards,
Uri Bar


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.