I have encountered the same problem running off an oracle database. I have played around with the query directly a SQL client and have come to the conclusion that you cannot order by fields that are not in the select statment IF you are using distinct in the select clause
e.g
Code:
select
distinct
NICKNAME
from
PEOPLE
order by
LASTNAME,
FIRSTNAME
won't work
Code:
select
NICKNAME
from
PEOPLE
order by
LASTNAME,
FIRSTNAME
will work but could result in duplicate entries
Code:
select
distinct
LASTNAME,
FIRSTNAME,
NICKNAME
from
PEOPLE
order by
LASTNAME,
FIRSTNAME
will work but you have to add the fields being ordered by in the select clause.
All of this also applies to any fields in joined tables as well.
The problem with using hibernate and joined tables is that hibernate wants to do lazy loading and use proxied objects. This means that in it's initial select, it will want to select the data from the parent object/table ONLY, and it will want to ensure that it gets a uniqe list (using distinct) this means that in HQL if you are joining any tables you cannot order by any of the child objects.
The problem in doing it the ordering at the Java level is that it then forces me to pull all my data initially, and I loose the benefits of paging, as well as the lazy loading.
Does this make sence to anyone? Please someone tell me I am wrong about this as I would like to find a way to do this sorting at the database level.