Reputation: 658
In a hibernate transaction, I am updating a row (by session.update(domainObject)
) and then doing a select count(*)
with a where condition on the same table along with a join on some other table. What I am observing(via logs) is that the update operation is happening at the end of the transaction and hence the select query which is supposed to execute after the update query shows the incorrect result. I am not able to understand why is that happening. Any leads?
Upvotes: 1
Views: 722
Reputation: 26522
When you perform a SELECT
, the persistence provider determines whether it should flush the previous CRUD operations that were performed previously in the same transaction.
This is to make sure that the returned result is correct.
In theory, when you make a SELECT that contains a reference to an entity that was modified in the current session, a flush should occur.
You may want to try to:
a) Set the flush mode to: <property name="org.hibernate.flushMode" value="AUTO"/>
.
This should be the default but you may be overriding this. This is to ensure that:
The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.
b) Set the flush mode to: <property name="org.hibernate.flushMode" value="ALWAYS"/>
.
Be careful with this as:
The Session is flushed before every query.
c) Do a manual session.flush()
before that query (last resort)
Upvotes: 1