Problems with Hibernate SQL statements logging

15 views Asked by At

I'm facing one problem with Hibernate logging. If I enable org.hibernate.SQL to debug I can see the native SQL statements in the defined logger. This works in every scenario but the one when the HQL has collections fetches.

Digging the code I could find where the execution behaves differently and it might cause the issue. In class org.hibernate.hql.internal.ast.QueryTranslatorImpl there's the block

        if ( hasLimit && containsCollectionFetches() ) {
            boolean fail = session.getFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
            if (fail) {
                throw new HibernateException("firstResult/maxResults specified with collection fetch. " +
                    "In memory pagination was about to be applied. " +
                    "Failing because 'Fail on pagination over collection fetch' is enabled.");
            }
            else {
                LOG.firstOrMaxResultsSpecifiedWithCollectionFetch();
            }
            RowSelection selection = new RowSelection();
            selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
            selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
            queryParametersToUse = queryParameters.createCopyUsing( selection );
        }
        else {
            queryParametersToUse = queryParameters;
        }

When the execution get's in the if ( hasLimit && containsCollectionFetches() ) then the Log of statement done in org.hibernate.engine.jdbc.spi.SqlStatementLogger doesn't work. This is the method where statements are logged

@AllowSysOut
public void logStatement(String statement, Formatter formatter) {
    if ( logToStdout || LOG.isDebugEnabled() ) {
        if ( format ) {
            statement = formatter.format( statement );
        }
        if ( highlight ) {
            statement = FormatStyle.HIGHLIGHT.getFormatter().format( statement );
        }
    }
    LOG.debug( statement );
    if ( logToStdout ) {
        String prefix = highlight ? "\u001b[35m[Hibernate]\u001b[0m " : "Hibernate: ";
        System.out.println( prefix + statement );
    }
}

and the statement that sometimes doesn't work is LOG.debug( statement );

I inspected the LOG object and the logger and levels were correct (org.hibernate.SQL and DEBUG) Has anybody had a similar issue?

It's not a blocking issue but for performance analysis is very useful to have the SQL statement, especially for cases with collection fetches.

Many thanks for any help

0

There are 0 answers