Roman Zenka Roman Zenka - 1 year ago 91
Java Question

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

I have a persistent hibernate object I obtained using

I changed it since.

I want to execute
to save memory, as I am done with the object.

The documentation to
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
but that would flush all changes.

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

Answer Source

Call 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download