BasicFormatterImpl throws a NPE if native SQL begins with a parentheses

Description

If a native query starts with a parentheses and Hibernate is logging queries, BasicFormatterImpl throws a NPE because it is assuming that the previous token was not null.

An example of SQL that may begin with parentheses is a UNION:

(SELECT ....)
UNION
(SELECT ...)

Below is the stacktrace.

java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.isFunctionName(BasicFormatterImpl.java:359)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.openParen(BasicFormatterImpl.java:340)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.perform(BasicFormatterImpl.java:141)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl.format(BasicFormatterImpl.java:75)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:89)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:77)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:167)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1985)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1893)
at org.hibernate.loader.Loader.doQuery(Loader.java:938)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2692)
at org.hibernate.loader.Loader.doList(Loader.java:2675)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
at org.hibernate.loader.Loader.list(Loader.java:2502)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2162)
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
at org.hibernate.query.Query.getResultList(Query.java:146)

Attachments

1

Activity

Show:

Vlad MihalceaNovember 1, 2017 at 4:00 PM

Applied PR upstream.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created November 1, 2017 at 1:09 PM
Updated January 14, 2025 at 7:21 PM
Resolved November 1, 2017 at 4:00 PM