如何在JBoss 7.1.1中启用查询缓存

时间:2014-02-06 10:33:40

标签: java caching jpa jboss infinispan

我试图在JBoss 7.1.1中没有运气启用查询缓存 我在代码中添加了这个:

TypedQuery<Currency> query = entityManager.createNamedQuery("getCurrency",Currency.class);
query.setParameter("code", code);
query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
query.setHint("org.hibernate.cacheable", true); 

namedQuery看起来像这样:

@Cacheable
@Entity
@Table(name = "currency")
@NamedQuery(
    name = "getCurrency",
    query = "FROM Currency c WHERE c.iso4217code = :code"
)

我在persistence.xml中有以下内容:

<persistence-unit name="cache_persistence">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:jboss/datasources/cache</jta-data-source>
    <class>com.unwire.cache.model.CacheTest</class>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
     <property name="hibernate.show_sql" value="false" />
     <property name="hibernate.hbm2ddl.auto" value="none" />
     <property name="hibernate.cache.use_second_level_cache" value="true"/>
     <property name="hibernate.cache.use_query_cache" value="true"/>
     <property name="hibernate.generate_statistics" value="true"/>
    </properties>

</persistence-unit>

当我在服务器上运行war文件时 实体被缓存但从未使用查询缓存

我已在此处上传文件:http://www.filedropper.com/cachetest

1 个答案:

答案 0 :(得分:1)

在我的代码中(ear archive,但这不应该有所作为)我只是:

query.setHint("org.hibernate.cacheable", true); 
query.setHint("org.hibernate.cacheMode", "NORMAL"); 

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  <properties>
  <property name="hibernate.cache.use_second_level_cache" value="true"/>
  <property name="hibernate.cache.use_query_cache" value="true"/>
  <property name="hibernate.generate_statistics" value="true" />
</properties>

并且没有任何额外的东西(就像你有的)并且它有效。当然,您必须针对查询的相同参数测试缓存。