databasehibernateormobject-relational-model

How to get updated objects instantaneously by Hibernate?


I'm not getting updated objects retrieved by hibernate session. e.g. If I update an object by Session B [the object was already fetched earlier by Session A], then I'm not getting the updates made by Session B instantaneously.

Then I've to use the following code to force Hibernate fetch the updated values.

session.evict(user);

But that involves round-trip to database. How I can avoid that? Is there more appropriate way to get the updates instantaneously?


Solution

  • By design hibernate sessions (persistence context) are isolated from one another, and guarantee repeatable reads.

    Better way to handle your issue is call session.refresh() on session A if you suspect that you object is stale, rather than calling evict() and get()