Reputation: 49
For example, instead of
@Modifying(flushAutomatically = true)
Only
@Modifying
I do understand the main point of it - to flush all cache to DB before querying. But after we write @Query, isn't JPQL query already processed by JPA? And JPA has default Flushmode = AUTO, so it should flush data to db anyway.
Am I thinking right? If yes, can I skip this annotation?
P.S. I haven't found anything on Stack Overflow that is answering that question, so it is not a duplicate.
Upvotes: 2
Views: 2899
Reputation: 13041
As it's discussed in this thread:
@Modifying
annotation hasclearAutomatically
attribute which defines whether it should clear the underlying persistence context after executing the modifying query....
When executing modifying queries with this attribute activated, it drops all non-flushed changes still pending in the
EntityManager
....
Is it possible to add a new attribute to
@Modifying
annotation calledflushAutomatically
to automatically flush all non-persisted changes to the underlying context before executing the modifying query?
and further
JPA’s
AUTO
FlushModeType
and Hibernate: if the current executed query is not going to hit the pending SQL INSERT/UPDATE/DELETE statements then the flush is not strictly required.As stated in the reference documentation, the
AUTO
flush strategy may sometimes synchronize the current persistence context prior to a query execution.The
clearAutomatically
property drops all the pending changes in the EntityManager that are not related to the current update query (cause they are not automatically flushed).That's why I'm asking for a new property to force the
EntityManager
to flush changes.
So, this property have sense only when you use also clearAutomatically
property: @Modifying(clearAutomatically = true, flushAutomatically = true)
.
Upvotes: 1