-->
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: Question about using of CreateSQLQuery with scalar values
PostPosted: Wed Oct 19, 2005 12:19 pm 
Newbie

Joined: Tue Oct 18, 2005 5:47 pm
Posts: 1
hi,

I am using Hibernate 2.1.7 with DB2 UDB.

I need to execute a SQL Query (in order to use sql functions like COALESCE or CASE ... WHEN .. THEN).

So I tried to use the CreateSQLQuery() statement like this
requete = new StringBuffer()
.append(" select ")
.append(" {declaca0.*} ")
.append(" from DECLAFOU.DECLACA {declaca0} ")
.append(" where {declaca0}.AACC_NUMERO='1' ");
Query quer = session.createSQLQuery( requete.toString(),
new String[] {"declaca0"},
new Class[] {Declaca.class});

It works fine when the request retrieves full managed entity objects.

But when I want to select only few columns, it throws me an "Invalid column name" exception.

Maybe i don't use createSQLQuery in the rigth way. Or maybe there is an easiest way to execute SQL select directly (with callBacks??).

I read in this forum that it is possible to addScalar to the query but I can't find this method in the Query class.

In fact, i am a bit lost :-((

Thx in advance for any help


Top
 Profile  
 
 Post subject: Use alias
PostPosted: Wed Oct 19, 2005 2:49 pm 
Newbie

Joined: Wed Sep 17, 2003 3:31 am
Posts: 10
Location: Napoli (IT)
The manual says:
Quote:
"you may list the columns explicity, but even then you must let Hibernate inject the SQL column aliases for each property. The placeholder
for a column alias is just the property name qualified by the table alias.


That is, for each column you want to use in the SQL command, you must alias the property so that hibernate put the real column name taken from mapping.

Let's say column AACC_NUMERO is mapped in property 'numero', then you'll have:

Code:

select numero as {declaca0.numero}
   from {declaca0}
   where numero ='1'



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.