Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: evict() a persistent object while storing its changes

I have a persistent hibernate object I obtained using session.save(object)

I changed it since.

I want to execute session.evict(object) to save memory, as I am done with the object.

The documentation to evict() here states that changes to the object will not be persisted. In other words - evict will cause me to lose changes I did to the object.

I could call session.flush() but that would flush all changes.

How can I persist changes made to a single persistent object before eviction?

like image 222
Roman Zenka Avatar asked Jul 26 '11 20:07

Roman Zenka


People also ask

What does hibernate evict do?

evict() To detach the object from session cache, hibernate provides evict() method. After detaching the object from the session, any change to object will not be persisted. The associated objects will also be detached if the association is mapped with cascade="evict".

What is the difference between evict () and clear ()?

evict() evicts a single object from the session. clear() evicts all the objects in the session. Calling clear() is like calling evict() on every object associated with the session.

What is difference between hibernate save () saveOrUpdate () and persist () methods?

Difference between save and saveOrUpdate in Hibernatesave() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.

How do you persist objects in hibernate?

All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table. All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.


2 Answers

Call session.save(object) or session.saveOrUpdate(object), then you can call evict on it if you must. However, you must flush the session before doing this. The session is designed to be a unit-of-work based interface, for good reason -- transactional semantics would be a disaster without this feature. If you need to flush individual entities without flushing others that are part of the same session, you need to rethink your unit-of-work.

like image 78
jonathan.cone Avatar answered Sep 21 '22 12:09

jonathan.cone


Doing session.evict(obj) will remove the object instance from the session cache. Therefore if you are saving the object for the first time, you will have to explicitly commit via session.save(obj) before evicting the object from the cache. Subsequent update calls should follow through session.saveOrUpdate(obj) or session.update(obj) before calling evict to remove the loaded object from the cache.

In doing so means you will have to explicitly call session.load(obj) or session.get(obj) when you need the instance of the object back in the cache.

like image 31
Bitmap Avatar answered Sep 21 '22 12:09

Bitmap