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

前端 未结 3 476
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 03:38

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

I changed it since.

I want to execute session.evict(object)

相关标签:
3条回答
  • 2020-12-31 04:17

    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.

    0 讨论(0)
  • 2020-12-31 04:30

    Couldn't you manually save the object first via something like

    session.saveOrUpdate(theObject);
    session.evict(theObject);
    
    0 讨论(0)
  • 2020-12-31 04:32

    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.

    0 讨论(0)
提交回复
热议问题