SQLQuery nativeQuery = ...;
final Collection<String> spaces = nativeQuery.getSynchronizedQuerySpaces();
final boolean autoFlush = isEnabled( "hibernate.auto_flush_before_native_query", false );
if ( autoFlush && spaces.isEmpty() ) {
flush();
}
else {
autoFlushIfRequired( spaces );
}