-->
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: Problem with column alias and order by clause
PostPosted: Sun May 14, 2006 7:50 am 
Newbie

Joined: Sun May 14, 2006 7:27 am
Posts: 2
Location: Sheffield, UK.
The following very simple example works as expected:

Code:
SELECT user.id, user.name FROM User AS user ORDER BY user.id

However, if I introduce a column alias in the SELECT clause and try and use it in the ORDER BY clause, it fails:

Code:
SELECT user.id AS userId FROM User AS user ORDER BY userId

When I look at the SQL that has been generated, Hibernate changes the userId alias in the SELECT to some internal identifier (col_1_0_), but does not use the identifier in the ORDER BY clause, leaving the database to try and ORDER BY the original userId alias that no longer exists in the query.

Given that the parser allows the query, I've assumed that it is legitimate to alias the column like this, although I've not seen this documented in the Hibernate manual. Obviously, this is a silly example - in real life, I have a sub-query as one of the columns in the select, and have to alias it in order to sort by it in the order-by clause.

Should I be able to do this? If so, I'll isolate this into a test case and submit to Jira; at the moment all of this is entagled in my application.

FYI, the fairly unpleasant workarounds are to either use the internal identifier in the ORDER BY clause:

Code:
SELECT user.id AS userId FROM User AS user ORDER BY col_1_0_

Or revert to SQL.

I am using Hibernate 3.1.3, and MySQL 5.

Thanks in advance for any help, Alex.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 12:05 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
This has already been reported and, according to Gavin, is not a bug.

http://opensource.atlassian.com/project ... se/HHH-892

I don't understand why the alias can't be used if the SQL dialect allows it though.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 1:21 pm 
Newbie

Joined: Sun May 14, 2006 7:27 am
Posts: 2
Location: Sheffield, UK.
Thanks. I had checked Jira, but filtered on bugs only.

I'm a bit confused as to why this is not a bug if HSQL supports "SELECT x AS y".


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.