Hi, I'm new to this forum, so hello to everyone!
I'm using Hibernate 3.6.2.
I have have a very simple SQL query trying to get the max of some column in a table (just a table of coordinates x,y,z..)
Code:
HibernateUtil.getCurrentSession().createSQLQuery("select max(x), max(y), max(z) from locations").uniqueResult();
Executing this returns an Object[] with 3 values each equals to the first max(x), say [95, 95, 95] while the correct result would be, let's say, [95, 13, 1]
If I assign an alias to each column in the sqlquery the result is correct:
Code:
HibernateUtil.getCurrentSession().createSQLQuery("select max(x) as x, max(y) as y, max(z) as z from locations").uniqueResult();
I've searched in code and I found that in class org.hibernate.type.descriptor.sql.IntegerTypeDescriptor is called this method:
Code:
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicExtractor<X>( javaTypeDescriptor, this ) {
@Override
protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( rs.getInt( name ), options );
}
};
}
If I don't assign an alias to columns then all 3 aliases are populated with "", so this line:
Code:
return javaTypeDescriptor.wrap( rs.getInt( name ), options );
calling a ResultSet method gives the first column with the specified alias (which is equals in all 3 columns).
Now, I've learned the lesson and I put everytime an alias when executing some query, but could we say this is a bug?
Thanks for your opinion...
Luca