On execution of a Query, Hibernate will perform an "auto flush" based on the "query spaces" associated with the Query. The query spaces are the table names to which the query refers. E.g., given the HQL `from MyEntity ...` Hibernate knows that the table spaces are the tables to which `MyEntity` is mapped.
During the auto flush, Hibernate will check whether there are pending affected by those table spaces, and if so automatically flush those changes.
If the query is a mutation query (update, delete) there is another piece to this. Any second level cache regions that are affected by these query spaces need to be invalidated - we do not know which rows were changed/deleted and if changed what the new values are. So we need to invalidate those regions.
No query spaces means essentially "flush all" and for mutation queries "invalidate all". No query spaces is the normal state of all native (SQL) queries. Hibernate does not parse the SQL as it would need to in order to be able to discover all tables. For NativeQuery, it is expected that the user will tell us the names of the affected query space(s).
However, specifying the affected query spaces requires using Hibernate-specific API casting the NativeQuery to `SynchrnoizeableQuery` and using one of the methods for adding the spaces.
Would be great to define a query hint that can have the same effect in a strictly JPA manner (API wise anyway).