Java 使用条件休眠查询缓存
我想有一点我还不明白。 我有一个类,它在属性中有3个元素集合Java 使用条件休眠查询缓存,java,mysql,hibernate,ehcache,Java,Mysql,Hibernate,Ehcache,我想有一点我还不明白。 我有一个类,它在属性中有3个元素集合 public class Pays implements Serializable{ private static final long serialVersionUID = -8767337896773261244L; /** * the id of the country */ private long id; /** * name of the country */ private String nom; /** *
public class Pays implements Serializable{
private static final long serialVersionUID = -8767337896773261244L;
/**
* the id of the country
*/
private long id;
/**
* name of the country
*/
private String nom;
/**
* The region of the country
*/
private Region region;
/**
* Relations between a country and a composant
*/
private Set<PaysComposants> pc = new HashSet<PaysComposants>(0);
/**
* Relations between a country and a service
*/
private Set<PaysServices> ps = new HashSet<PaysServices>(0);
/**
* Relations between a country and some keys figures
*/
private Set<KeyFigure> kf = new HashSet<KeyFigure>(0);
实际上,唯一放在缓存中的对象是Pays p,其他集合pc、ps和kf没有放在缓存中
这是我的ehCache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<defaultCache maxElementsInMemory="100000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" />
<cache name="org.hibernate.cache.internal.StandardQueryCache"
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="3600">
</cache>
<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxElementsInMemory="10000" eternal="true">
</cache>
我的hibernate属性
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.generate_statistics">true</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">
applicationContext-ehCache-entity.xml
</prop>
</props>
</property>
org.hibernate.dialogue.mysqldialogue
真的
更新
线
真的
真的
真的
真的
org.hibernate.cache.EhCacheProvider
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
applicationContext-ehCache-entity.xml
还有我对专家的依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.2.0.Final</version>
</dependency>
org.hibernate
休眠ehcache
4.2.0.4最终版本
我怎样才能做到这一点?我使用FetchMde.Join是因为我想避免n+1选择,实际上我的支付是在一个选择中加载的。
提前感谢。Hibernate将只缓存Pays p对象,这是正确的,因为仅在该对象上创建条件。如果要缓存其他集合,则必须使用集合属性的getter方法上的Cachebale。
您还可以在每个实体对象上使用缓存注释。我解决了我的问题,只需在my web.xml中应用一个过滤器,如下所示:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>baselineSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
openSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
sessionFactoryBeanName
基线会话工厂
openSessionInViewFilter
/*
我已经在每个实体和每个集合的getter上添加了这个@Cache(usage=cacheconcurrencysttrategy.READ\u WRITE)
,但它似乎不起作用。您是否使用Spring缓存或EHCaching?是的,我编辑了我的问题,并添加了我的ehcache.xml、我的hibernate属性和我的maven依赖项。
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>baselineSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>