steve wrote:
Please post the AST trees for that query... (logger = org.hibernate.hql.ast.AST).
HQL Query: "select ei.empId, ei.empName from EmpInfo as ei order by col_1_0_"
Generated SQL: "Hibernate: select empinfo0_.EMP_ID as col_0_0_, empinfo0_.EMP_NAME as col_1_0_ from EMP_INFO empinfo0_ order by col_1_0_"
AST tree:
DEBUG http-8080-Processor24 org.hibernate.hql.ast.AST - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'from'
| | \-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'adestagroup'
| | | | | | \-[IDENT] 'intercom'
| | | | | \-[IDENT] 'tac'
| | | | \-[IDENT] 'bo'
| | | \-[IDENT] 'EmpInfo'
| | \-[ALIAS] 'ei'
| \-[SELECT] 'select'
| +-[DOT] '.'
| | +-[IDENT] 'ei'
| | \-[IDENT] 'empId'
| \-[DOT] '.'
| +-[IDENT] 'ei'
| \-[IDENT] 'empName'
\-[ORDER] 'order'
\-[IDENT] 'col_1_0_'
DEBUG http-8080-Processor24 org.hibernate.hql.ast.AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (EMP_INFO)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DOT] DotNode: 'empinfo0_.EMP_ID' {propertyName=empId,dereferenceType=4,propertyPath=empId,path=ei.empId,tableAlias=empinfo0_,className=com.adestagroup.intercom.tac.bo.EmpInfo,classAlias=ei}
| | +-[ALIAS_REF] IdentNode: 'empinfo0_.EMP_ID' {alias=ei, className=com.adestagroup.intercom.tac.bo.EmpInfo, tableAlias=empinfo0_}
| | \-[IDENT] IdentNode: 'empId' {originalText=empId}
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_0_'
| +-[DOT] DotNode: 'empinfo0_.EMP_NAME' {propertyName=empName,dereferenceType=4,propertyPath=empName,path=ei.empName,tableAlias=empinfo0_,className=com.adestagroup.intercom.tac.bo.EmpInfo,classAlias=ei}
| | +-[ALIAS_REF] IdentNode: 'empinfo0_.EMP_ID' {alias=ei, className=com.adestagroup.intercom.tac.bo.EmpInfo, tableAlias=empinfo0_}
| | \-[IDENT] IdentNode: 'empName' {originalText=empName}
| \-[SELECT_COLUMNS] SqlNode: ' as col_1_0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[ei], fromElementByTableAlias=[empinfo0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'EMP_INFO empinfo0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=ei,role=null,tableName=EMP_INFO,tableAlias=empinfo0_,origin=null,colums={,className=com.adestagroup.intercom.tac.bo.EmpInfo}}
\-[ORDER] OrderByClause: 'order'
\-[IDENT] IdentNode: 'col_1_0_' {originalText=col_1_0_}
------------------------------------------------------------------------------------
I tried query with aliases for selected attributes such as:
"select ei.empId as e1, ei.empName as e2 from EmpInfo as ei order by e2"
But, again Hibernate still resolved the individual attributes as col_0_0_, col_1_0_ and so on...
Hibernate: select empinfo0_.EMP_ID as col_0_0_, empinfo0_.EMP_NAME as col_1_0_ from EMP_INFO empinfo0_ order by e2
and I got the exception:
WARN http-8080-Processor25 org.hibernate.util.JDBCExceptionReporter - SQL Error: -208, SQLState: 42707
ERROR http-8080-Processor25 org.hibernate.util.JDBCExceptionReporter - [SQL0208] ORDER BY column E2 or expression not in result table.
Caused by: java.sql.SQLException: [SQL0208] ORDER BY column E2 or expression not in result table.
AST Tree:
DEBUG http-8080-Processor25 org.hibernate.hql.ast.AST - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'from'
| | \-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'adestagroup'
| | | | | | \-[IDENT] 'intercom'
| | | | | \-[IDENT] 'tac'
| | | | \-[IDENT] 'bo'
| | | \-[IDENT] 'EmpInfo'
| | \-[ALIAS] 'ei'
| \-[SELECT] 'select'
| +-[AS] 'as'
| | +-[DOT] '.'
| | | +-[IDENT] 'ei'
| | | \-[IDENT] 'empId'
| | \-[IDENT] 'e1'
| \-[AS] 'as'
| +-[DOT] '.'
| | +-[IDENT] 'ei'
| | \-[IDENT] 'empName'
| \-[IDENT] 'e2'
\-[ORDER] 'order'
\-[IDENT] 'e2'
DEBUG http-8080-Processor25 org.hibernate.hql.ast.AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (EMP_INFO)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DOT] DotNode: 'empinfo0_.EMP_ID' {propertyName=empId,dereferenceType=4,propertyPath=empId,path=ei.empId,tableAlias=empinfo0_,className=com.adestagroup.intercom.tac.bo.EmpInfo,classAlias=ei}
| | +-[ALIAS_REF] IdentNode: 'empinfo0_.EMP_ID' {alias=ei, className=com.adestagroup.intercom.tac.bo.EmpInfo, tableAlias=empinfo0_}
| | \-[IDENT] IdentNode: 'empId' {originalText=empId}
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_0_'
| +-[DOT] DotNode: 'empinfo0_.EMP_NAME' {propertyName=empName,dereferenceType=4,propertyPath=empName,path=ei.empName,tableAlias=empinfo0_,className=com.adestagroup.intercom.tac.bo.EmpInfo,classAlias=ei}
| | +-[ALIAS_REF] IdentNode: 'empinfo0_.EMP_ID' {alias=ei, className=com.adestagroup.intercom.tac.bo.EmpInfo, tableAlias=empinfo0_}
| | \-[IDENT] IdentNode: 'empName' {originalText=empName}
| \-[SELECT_COLUMNS] SqlNode: ' as col_1_0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[ei], fromElementByTableAlias=[empinfo0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'EMP_INFO empinfo0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=ei,role=null,tableName=EMP_INFO,tableAlias=empinfo0_,origin=null,colums={,className=com.adestagroup.intercom.tac.bo.EmpInfo}}
\-[ORDER] OrderByClause: 'order'
\-[IDENT] IdentNode: 'e2' {originalText=e2}
-------------------------------------------------------------------------
Steve, what I am trying to say is Hibernate automatically generates aliases for the columns and it always overrides any user defined aliases, that's why e2 is being shown as an unknown column which was the similar issue on the JIRA, even though it was for an aggregate column. But this behaviour is inconsistent because there were situations in my project when hibernate generated aliases like survey2_, etc... which is similar to the comment posted by Jere Krischel on the same JIRA when HQL generated alias was LASTNAME2_.