In this release of JBoss EAP 6 a native SQL query which specifies a column multiple times without unique aliases (e.g. "select e.id, e.title, e.id from Event e") for any simple entity (e.g. Event) fails in with the following message:
----
org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query
----
This happens because Hibernate obtains the column labels from java.sql.ResultSetMetaData.getColumnLabel(). Hibernate cannot determine if the columns with the same name refer to the same value from the same entity.
Rather than possibly making an incorrect assumption, Hibernate throws the exception.
The workaround is to provide unique aliases for columns with the same name (e.g. "select e.id AS id1, e.title, e.id AS id2 from Event e").
Created attachment 910870[details]
Maven based test
Description of problem:
For any simple entity (e.g. Event) a native SQL query which specifies a column multiple times (e.g. "select e.id, e.title, e.id from Event e") fails in Hibernate 4:
org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query
Version-Release number of selected component (if applicable):
Hibernate 4
How reproducible:
Consistently
Steps to Reproduce:
For a simple entity, execute a native SQL query specifying one column (e.g. 'id') multiple times
List results = session.createSQLQuery(
"select e.id, e.title, e.id from Event e").list();
Actual results:
org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:630)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2039)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
at org.hibernate.loader.Loader.doQuery(Loader.java:899)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2516)
at org.hibernate.loader.Loader.doList(Loader.java:2502)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
at org.hibernate.loader.Loader.list(Loader.java:2327)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1826)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
...
Expected results:
The exception is not expected.
Additional info:
- The same query works as late as in Hibernate 3.3.2.GA.
- Modifying the query to use explicit column aliases works around the issue:
"select e.id as eid1, e.title, e.id as eid2 from Event e"
Created attachment 910870 [details] Maven based test Description of problem: For any simple entity (e.g. Event) a native SQL query which specifies a column multiple times (e.g. "select e.id, e.title, e.id from Event e") fails in Hibernate 4: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query Version-Release number of selected component (if applicable): Hibernate 4 How reproducible: Consistently Steps to Reproduce: For a simple entity, execute a native SQL query specifying one column (e.g. 'id') multiple times List results = session.createSQLQuery( "select e.id, e.title, e.id from Event e").list(); Actual results: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:630) at org.hibernate.loader.Loader.getResultSet(Loader.java:2039) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811) at org.hibernate.loader.Loader.doQuery(Loader.java:899) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) at org.hibernate.loader.Loader.doList(Loader.java:2516) at org.hibernate.loader.Loader.doList(Loader.java:2502) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332) at org.hibernate.loader.Loader.list(Loader.java:2327) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1826) at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:231) at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157) ... Expected results: The exception is not expected. Additional info: - The same query works as late as in Hibernate 3.3.2.GA. - Modifying the query to use explicit column aliases works around the issue: "select e.id as eid1, e.title, e.id as eid2 from Event e"