Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在hibernate中使用二级实体缓存来缓存整个表。?_Hibernate_Ehcache_Second Level Cache - Fatal编程技术网

如何在hibernate中使用二级实体缓存来缓存整个表。?

如何在hibernate中使用二级实体缓存来缓存整个表。?,hibernate,ehcache,second-level-cache,Hibernate,Ehcache,Second Level Cache,一个多月来,我一直在为这个问题拼命工作。请帮忙 我想在hibernate中为几个masters表执行二级实体缓存(使用EHcache)。我遵循了和几个其他链接,并按照以下方式进行了配置: 对于cfg文件 <!-- For Second Level Caching --> <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property> <

一个多月来,我一直在为这个问题拼命工作。请帮忙

我想在hibernate中为几个masters表执行二级实体缓存(使用EHcache)。我遵循了和几个其他链接,并按照以下方式进行了配置:

对于cfg文件

    <!-- For Second Level Caching -->

    <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
    <property name="hibernate.show_sql">true</property>
     <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_query_cache">false</property> 
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <property name = "net.sf.ehcache.configurationResourceName">/ehcache.xml</property> 

    <!-- For statistics -->

    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.cache.use_structured_entries">true</property>
    <property name = "net.sf.hibernate.cache.UpdateTimestampsCache ">true</property>        

org.hibernate.dial.DB2Dialect
真的
真的
假的
org.hibernate.cache.EhCacheProvider
/ehcache.xml
真的
真的
真的
hbm文件

    <hibernate-mapping>
        <class name="com.example.ehcacheSample.stateMstr" table="STATE_MSTR">

        <cache include="all" usage="read-only" region = "stateMstr"/>

        ...........       
        </class>
    </hibernate-mapping>

...........       
ehcache.xml

    <cache name = "stateMstr"
        maxElementsInMemory="1"     
        eternal="false"   
        timeToIdleSeconds="300"   
        timeToLiveSeconds="600"
        overflowToDisk= "true" />

关于这一点,我有一个问题

  • 文档说,对于二级缓存,只需要使用主键加载实体。这意味着为了在缓存中获得一个包含100行的表,我们必须使用列的主键作为可序列化id运行循环100次。 换句话说,如果我想在缓存中创建表的副本,那么逐行加载实体是唯一的选项吗

  • 第二,如果我们想使用同一行的任何列的给定值从缓存中获取行的主Id,那么在不命中数据库查询的情况下,这是可能的吗? 例如,如果deptt名称已知,并且我想获取deptt的id,则会缓存deptt表。。是否可以在不点击数据库查询的情况下获取id


我和你一样有一些问题

首先,如果要加载表的所有行,可以使用查询缓存(将hibernate配置更改为true)

现在在查询缓存中有一个查询和查询结果(结果是Depp(所有表)的列表)

如果要使用此列表,则必须使用相同的查询再次获取Depp列表,而不必访问数据库

session.createQuery("from deptt").setCacheable(true);

您将结果放入一个集合、列表。。。如果你想得到一个Depp,你可以从你拥有的Depp列表中得到它

对不起。。。可设置缓存(true)和不可设置查询(true)谢谢比尔的回复。我也尝试过这样做,但我转向实体缓存的唯一原因是我在许多链接中读到实体缓存优于查询缓存。第二,我甚至看到,每当执行查询缓存时,都会在外部位置创建一个新文件夹,而在实体缓存中,每次执行单个文件时都会替换它。应该避免查询缓存,因为它会迅速增大缓存的大小
session.createQuery("from deptt").setCacheable(true);
session.createQuery("from deptt").setCacheable(true);