I have loaded an entity into my transaction and have changed a property of that entity. The transaction is not yet commited. Now I would like to get the original value of the changed property.
I’ve tried with a HQL query like select p.property from Person p where p.id = 1 with the ID of the entity loaded in the transaction.
I’ve set query.setHint("org.hibernate.cacheMode", CacheMode.IGNORE); before executing the query. But no success. Hibernate returns the value as set in the current transaction, not the one from the database.
Is there any way around this?
In short: track the old value yourself.
Hibernate loads a unique version of an entity into the session (the first level cache) for a given database identifier. This won’t work.
This hint is used to affect the query cache (that rely on the second-level-cache), this won’t affect your current “issue”.
Either
session.refresh()to force a reload of your entity (and you’ll loose the changes)