Hibernate 已配置的二级缓存和数据未从二级缓存加载
我在hibernate.cfg.xml、Ehcache.xml中使用Ehcache配置了二级缓存,并在映射文件中设置了Cache usage属性。并使用hibenrate statics从缓存或数据库加载数据。但它没有加载。它再次执行查询。我提到了代码Hibernate 已配置的二级缓存和数据未从二级缓存加载,hibernate,Hibernate,我在hibernate.cfg.xml、Ehcache.xml中使用Ehcache配置了二级缓存,并在映射文件中设置了Cache usage属性。并使用hibenrate statics从缓存或数据库加载数据。但它没有加载。它再次执行查询。我提到了代码 <hibernate-configuration> <session-factory> <property name="connection.username">pension2</propert
<hibernate-configuration>
<session-factory>
<property name="connection.username">pension2</property>
<property name="connection.password">pension2</property>
<property name="connection.url">jdbc:oracle:thin:@191.161.0.25:1521:pension</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="myeclipse.connection.profile">pension</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.region.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml </property>
<mapping resource="com/aims/mapping/Teacher.hbm.xml" />
<mapping resource="com/aims/mapping/Student.hbm.xml" />
<mapping resource="com/aims/mapping/Student_marks_detl.hbm.xml" />
<mapping resource="com/aims/mapping/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
但是每次执行createQuery时,我都配置了它,为什么它没有从缓存返回。配置二级缓存是否有任何错误。请帮助我>?二级缓存将ID映射到实体,因此,它仅在按id查询实体时使用。查询缓存将查询映射到查询检索到的一组实体id。因此,二级缓存和查询缓存实际上只有在一起使用时才有用 要启用查询缓存,仅将查询设置为可缓存是不够的,但还必须在Hibernate
会话工厂
配置中启用查询缓存:
<property name="hibernate.cache.use_query_cache">true</property>
true
希望这有帮助
<hibernate-mapping>
<class name="com.aims.beans.Teacher" table="teacher">
<cache usage="read-write" />
<id name="tno" column="tno" type="java.lang.Integer" >
<generator class="assigned" />
</id>
<property name="tname" type="java.lang.String" column="tname"/>
</class>
</hibernate-mapping>
public static long getHitCount() {
long hitcount = 0;
hitcount = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("com.aims.beans.Teacher")
.getHitCount();
return hitcount;
}
public static long getMissCount() {
long miscount = 0;
miscount = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("com.aims.beans.Teacher")
.getMissCount();
return miscount;
}
<property name="hibernate.cache.use_query_cache">true</property>