Hibernate中的一级缓存

Hibernate中的一级缓存

本文介绍了Hibernate中的一级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,正在阅读关于Hibernate First Level Cache的内容。
我有疑问。
与SessionFactory关联的每个会话是否都有单独的缓存,或者对于所有会话将只有一个缓存?
任何人都可以请解释这个 。

解决方案

一级缓存与Session对象关联。正如我们所知,会话对象是根据会话工厂的需求创建的,一旦会话关闭,它就会丢失。同样,与会话对象关联的第一级高速缓存仅在会话对象处于活动状态时才可用。它仅适用于会话对象,不能在应用程序的任何其他部分访问任何其他会话对象。



一些要点


  1. 一级缓存与会话对象相关联,应用中的其他会话对象无法看到它。


  2. 缓存对象的范围属于会话。一旦session被关闭,
    缓存的对象将永远消失。

  3. 第一级缓存默认启用,您不能禁用它。 >
  4. 当我们第一次查询实体时,它从数据库中检索并存储在与hibernate会话相关的第一级缓存中。 b

  5. 如果我们使用相同的会话对象再次查询同一对象,它将从缓存中加载并且不会执行sql查询。

  6. 使用evict()方法可以从会话中删除加载的实体。如果已经使用evict()方法删除了该实体,则该实体的下一次加载将再次进行数据库调用。 整个会话缓存可以使用清除() 方法。它将删除存储在缓存中的所有实体。 理解-hibernate-first-level-cache-with-example /rel =nofollow> http://howtodoinjava.com/2013/07/01/understanding-hibernate-first- level-cache-with-example /

    I am new to Hibernate and was reading about Hibernate First Level Cache.I have a doubt.Will every session that is associated with the SessionFactory have an individual cache or for all the sessions there will be only one cache?Can anyone please explain this .

    解决方案

    The first level cache is associated with Session object. As we know session object is created on demand from session factory and it is lost, once the session is closed. Similarly, first level cache associated with session object is available only till session object is live. It is available to session object only and is not accessible to any other session object in any other part of application.

    Some Important Points:

    1. First level cache is associated with "session" object and other session objects in application can not see it.

    2. The scope of cache objects is of session. Once session is closed,cached objects are gone forever.

    3. First level cache is enabled by default and you can not disable it.

    4. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.

    5. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.

    6. The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.

    7. The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.

    Quoted from: http://howtodoinjava.com/2013/07/01/understanding-hibernate-first-level-cache-with-example/

    这篇关于Hibernate中的一级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:44