Hey Guys, 
I'm having a problem with order by on hibernate I need to order by a sub-query aliased field but it is not using the proper alias so the sub-query aliased participants is generating a different sql alias for the selection but not for the sort by (which remains as participants) causing a SQL error.
Here are the queries
Code:
HQL:
select 
   event.slug, event.title, event.description, event.tags, event.startDate, event.endDate, event.status, event.type, event.createdAt, event.scheduledTo, event.photo.url, event.photo.createdAt, event.photo.title, event.photo.description, event.photo.tags, event.photo.type, event.address.latitude, event.address.longitude, event.address.postalCode, event.address.region, event.address.street, event.address.number, event.address.note, event.address.addressType, event.address.phone, event.address.phoneContact, event.address.website, event.address.neighborhood, event.address.city.name, event.address.city.state.name, event.address.city.state.abbreviation, event.address.city.state.country.name, (select count(*) from com.orion.dc.model.EventParticipant as eventParticipant where eventParticipant.event.key = event.key) as participants, event.photo.key, event.address.key, event.address.city.key, event.address.city.state.key, event.address.city.state.country.key, event.key 
from com.orion.dc.model.Event as event left join event.photo 
   left join event.address 
   left join event.address.city 
   left join event.address.city.state 
   left join event.address.city.state.country 
where 
   event.status = 'published' and (event.type = 'public') 
order by 
   order by participants
Generated SQL:
Code:
    select
        event0_.slug as col_0_0_,
        event0_.title as col_1_0_,
        event0_.description as col_2_0_,
        event0_.tags as col_3_0_,
        event0_.start_date as col_4_0_,
        event0_.end_date as col_5_0_,
        event0_.status as col_6_0_,
        event0_.type as col_7_0_,
        event0_.created_at as col_8_0_,
        event0_.scheduled_to as col_9_0_,
        photo1_.url as col_10_0_,
        photo1_.created_at as col_11_0_,
        photo1_.title as col_12_0_,
        photo1_.description as col_13_0_,
        photo1_.tags as col_14_0_,
        photo1_.type as col_15_0_,
        address2_.latitude as col_16_0_,
        address2_.longitude as col_17_0_,
        address2_.postal_code as col_18_0_,
        address2_.region as col_19_0_,
        address2_.street as col_20_0_,
        address2_.number as col_21_0_,
        address2_.note as col_22_0_,
        address2_.address_type as col_23_0_,
        address2_.phone as col_24_0_,
        address2_.phone_contact as col_25_0_,
        address2_.website as col_26_0_,
        address2_.neighborhood as col_27_0_,
        city4_.name as col_28_0_,
        state7_.name as col_29_0_,
        state7_.abbreviation as col_30_0_,
        country11_.name as col_31_0_,
        (select
            count(*) 
        from
            dc_events_event_participant eventparti42_ 
        where
            eventparti42_.event_id=event0_.event_id) as col_32_0_,
        event0_.photo_id as col_33_0_,
        event0_.address_id as col_34_0_,
        address2_.city_id as col_35_0_,
        city4_.state_id as col_36_0_,
        state7_.country_id as col_37_0_,
        event0_.event_id as col_38_0_ 
    from
        dc_events_event event0_ 
    left outer join
        dc_media_photo photo1_ 
            on event0_.photo_id=photo1_.photo_id 
    left outer join
        dc_geo_address address2_ 
            on event0_.address_id=address2_.address_id 
    left outer join
        dc_geo_city city4_ 
            on address2_.city_id=city4_.city_id 
    left outer join
        dc_geo_state state7_ 
            on city4_.state_id=state7_.state_id 
    left outer join
        dc_geo_country country11_ 
            on state7_.country_id=country11_.country_id 
    where
        event0_.status='published' 
        and event0_.type='public' 
    order by
        participants
could this be a bug on hibernate?
If I manually include the generated alias (aka col_32_0_) with the order by it works, but as I add new fields to the query that will change, so not a permanent solution.
Regards,
Ivanir Joao Kreuzberg