Issue Type: Bug Bug
Affects Versions: 4.1.9
Assignee: Unassigned
Components: core
Created: 31/Jan/13 1:39 AM
Description:

An SQLQuery surrounded by brackets:

sessionFactory.getCurrentSession().createSQLQuery("(select * from MyTable)").list();

gives this NPE:

java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.isFunctionName(BasicFormatterImpl.java:376)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.openParen(BasicFormatterImpl.java:357)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.perform(BasicFormatterImpl.java:156)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl.format(BasicFormatterImpl.java:91)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:101)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:95)

BasicFormatterImpl line 155 contains:

else if ( "(".equals( token ) ) { openParen(); }

The openParen function starts with this code:

private void openParen() { if ( isFunctionName( lastToken ) ... }

The "(" bracket is the first token in the query, the lastToken is therefore null and calling the isFunctionName with a null value causes the NPE.

Known workaround: enclose the query with a 2nd select * from : select * from (select * from MyTable) as MyAlias.

FYI, the query we used contained a union of 2 tables : (select * from MyTable union select * from MyTableCopy) order by MyColumn, hence requiring the brackets.

Environment: JUnit, H2 database with PostgreSQL mode, Spring 3.1.3
Project: Hibernate ORM
Priority: Minor Minor
Reporter: Koen Verrecken
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira