Executing a native query via the entityManager always does a flush()

We are moving from Hibernate 4 to Hibernate 5 with our move from JBoss 7 to Wildfly 10.

One thing we have noticed is that when we execute a native query via the entityManager, a flush() is always executed prior to the query execution regardless of whether the flush mode is set to MANUAL or not. This is not how it worked in Hibernate 4.

Is there a way to control this via configuration?

Thanks!

Cory.

One thing we have noticed is that when we execute a native query via the entityManager, a flush() is always executed prior to the query execution regardless of whether the flush mode is set to MANUAL or not. This is not how it worked in Hibernate 4.

That’s how JPA AUTO flush mode works and it’s better than the legacy AUTO mode which did not flush for native SQL queries. Not flushing for native SQL can break read-your-write consistency.

Now, if you set the flush mode to MANUAL and you still get an automatic flush, it means there is an issue. You need to replicate it and open a Jira issue.

Is there a way to control this via configuration?

To get the Hibernate-specific AUTO flush mode you need to bootstrap via Hibernate native mechanism, not via JPA. However, since you are using JBoss, I doubt you could do that. If you were using Spring, it might work if you use HibernateTransactionManager instead of JpaTransactionManager.

Yes we are running in MANUAL flush mode but the native query is still triggering a flush() prior to the native query execution.

Yes I agree this is a bug.

Ok. If ypu can replicate it with this test case template, then it’s a Hibernate bug.

If you can only replicate it with JBoss, then it’s an integration issue and need to be reported to JBoss issue tracker.