-->
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.  [ 1 post ] 
Author Message
 Post subject: SQL Query results haywire when columns have same alias
PostPosted: Mon Mar 31, 2008 10:52 am 
Newbie

Joined: Mon Oct 23, 2006 4:14 am
Posts: 2
When an SQL query contains multiple columns with the same alias the result will contain the same value for all the columns

Hibernate version: 3.2.6ga

Mapping documents: none used

Code between sessionFactory.openSession() and session.close():
Code:
SQLQuery query;
query = session.createSQLQuery("select 1 as col, 2 as col");

List<?> list;
list = query.list();

Object[] row;
row = (Object[]) list.get(0);

// Column 1 should contain a number with integer value of 1
assertEquals(1, ((Number) row[0]).intValue());
// Column 2 should contain a number with integer value of 2
assertEquals(2, ((Number) row[1]).intValue());


Full stack trace of any exception that occurs: none

Name and version of the database you are using: PosgreSQL 8.1 - 8.3

The generated SQL (show_sql=true):
Code:
Hibernate: select 1 as col, 2 as col


Debug level Hibernate log excerpt:
Code:
org.hibernate.impl.SessionImpl (<init>:219) - opened session at timestamp: 4943767363325952
org.hibernate.engine.query.QueryPlanCache (getNativeSQLQueryPlan:111) - unable to locate native-sql query plan in cache; generating (select 1 as col, 2 as col)
org.hibernate.impl.SessionImpl (listCustomQuery:1645) - SQL query: select 1 as col, 2 as col
org.hibernate.jdbc.AbstractBatcher (logOpenPreparedStatement:311) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
org.hibernate.jdbc.ConnectionManager (openConnection:415) - opening JDBC connection
org.hibernate.connection.DriverManagerConnectionProvider (getConnection:93) - total checked-out connections: 0
org.hibernate.connection.DriverManagerConnectionProvider (getConnection:99) - using pooled JDBC connection, pool size: 0
org.hibernate.SQL (log:346) - select 1 as col, 2 as col
Hibernate: select 1 as col, 2 as col
org.hibernate.jdbc.AbstractBatcher (getPreparedStatement:424) - preparing statement
org.hibernate.jdbc.AbstractBatcher (logOpenResults:327) - about to open ResultSet (open ResultSets: 0, globally: 0)
org.hibernate.loader.Loader (doQuery:682) - processing result set
org.hibernate.loader.Loader (doQuery:687) - result set row: 0
org.hibernate.loader.Loader (getRow:1164) - result row:
org.hibernate.type.IntegerType (nullSafeGet:122) - returning '2' as column: col
org.hibernate.type.IntegerType (nullSafeGet:122) - returning '2' as column: col
org.hibernate.loader.Loader (doQuery:709) - done processing result set (1 rows)
org.hibernate.jdbc.AbstractBatcher (logCloseResults:334) - about to close ResultSet (open ResultSets: 1, globally: 1)
org.hibernate.jdbc.AbstractBatcher (logClosePreparedStatement:319) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
org.hibernate.jdbc.AbstractBatcher (closePreparedStatement:470) - closing statement
org.hibernate.engine.StatefulPersistenceContext (initializeNonLazyCollections:748) - initializing non-lazy collections
org.hibernate.jdbc.JDBCContext (afterNontransactionalQuery:217) - after autocommit
org.hibernate.jdbc.ConnectionManager (aggressiveRelease:398) - aggressively releasing JDBC connection
org.hibernate.jdbc.ConnectionManager (closeConnection:435) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
org.hibernate.connection.DriverManagerConnectionProvider (closeConnection:129) - returning connection to pool, pool size: 1


The problem MAY HAVE been mentioned before in the http://forum.hibernate.org/viewtopic.php?p=2375594 post.
When using aggregate functions the returned columns commonly have an auto-generated alias that may be the same for different aggregations.

Commonly this problem arises when querying for IDs, as commonly all ID columns have the same name:
Code:
select order.id, line.id from order left join line on line.order_id = order.id where ...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.