休眠中的evict和detach有什么区别?

I'm having a bit of trouble understanding the difference between evict and detach, does evict detach the session anyway, if so what is the difference? I read it removes the object from the second level cache but didn't get it.

评论
  • 麻辣烫
    麻辣烫 回复

    Detach() is used when you want to make an entity unmanaged. You basically tell hibernate to stop tracking that entity modification so that if you update it's a field in any way you won't have those changes reflected on DB.

    session.evict() on the other hand is for removing an entity from the hibernate the first-level cache. If you load an entity from DB hibernate will store it in the cache and subsequent query will hit the cache first. Evicting the entity force hibernate to query DB again.