Sam*_*tra 5 caching hibernate jpa
我有一个名为Master_Info_tbl的表.它是一个查找表:
这是表的代码:
@Entity
@Table(name="MASTER_INFO_T")
public class CodeValue implements java.io.Serializable {
private static final long serialVersionUID = -3732397626260983394L;
private Integer objectid;
private String codetype;
private String code;
private String shortdesc;
private String longdesc;
private Integer dptid;
private Integer sequen;
private Timestamp begindate;
private Timestamp enddate;
private String username;
private Timestamp rowlastchange;
//getter Setter methods
Run Code Online (Sandbox Code Playgroud)
我有一个服务层调用方法
service.findbycodeType("Code1");
同样地,这个表也被查询其他代码类型,例如code2,code3等,直到code10从同一个表中获取结果集并显示在jsp页面的下拉列表中,因为这些下降是90%我想在全球范围内缓存它们的页面.
知道怎么做到这一点?
仅供参考:我正在使用带有Struts2和Spring的JPA和Hibernate.正在使用的数据库是DB2 UDB8.2
@Pascal
非常感谢您的所有回复.它帮助了我很多.我实现了我应该实现的一切(我想).我仍然不知道第二级缓存是否正常工作.因为我无法从缓存中查看log4j日志记录文件中的任何内容,也没有任何内容显示在控制台中.为了表明二级缓存实现工作,我需要有一些证据,需要向我的经理展示.所以我有点卡住了.
请帮忙!
我知道我非常接近完成它但只是....
这是我的代码(如果你认为某些东西丢失或者某些东西不应该在那里请告诉我):
实体类
@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "CODEVALUE_T")
public class CodeValue implements java.io.Serializable {
//all the field name with getter and setter
}
Run Code Online (Sandbox Code Playgroud)
persistence.xml中:
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
</properties>
Run Code Online (Sandbox Code Playgroud)
服务层DAO:
try {
System.out.println("Calling the findByCodetype() method");
final String queryString = "select model from CodeValue model where model.codetype"
+ "= :propertyValue" + " order by model.code";
Query query = em.createQuery(queryString);
query.setHint("org.hibernate.cacheable", true);
query.setParameter("propertyValue", propertyName);
return query.getResultList();
} catch (RuntimeException re) {
logger.error("findByCodetype failed: ", re);
throw re;
}
Run Code Online (Sandbox Code Playgroud)
Log4j.property值显示详细的
log4j.category.org.hibernate.cache = DEBUG
ehcache.xml代码(它的位置在我的struts.xml文件所在的src /文件夹中的相同位置)
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="6000"
timeToLiveSeconds="12000"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>
Run Code Online (Sandbox Code Playgroud)
Pas*_*ent 13
(...)因为这些下降是我想要在全球缓存它们的页面的90%.
使用查询缓存非常适合此用例.因此,激活二级缓存,缓存的CodeValue
实体(参见2.2.8.缓存的实体),并把查询背后findbycodeType
的查询缓存.为此,请使用:
javax.persistence.Query query = manager.createQuery(...);
query.setHint("org.hibernate.cacheable", true);
Run Code Online (Sandbox Code Playgroud)
为了澄清,查询和作为该查询的参数提供的值的组合用作键,值是该查询的标识符列表.示意性地,类似的东西:
*--------------------------------------------------------------------------* | Query Cache | |--------------------------------------------------------------------------| | [ "from CodeValue c where c.codetype=?", ["Code1"] ] -> [ 1, 2, ... ] | | [ "from CodeValue c where c.codetype=?", ["Code2"] ] -> [ 3, 5, 6, ... ] | *--------------------------------------------------------------------------*
因此,使用不同的参数调用您的方法不会"刷新以前的数据",该参数是键的一部分(或查询缓存不起作用).
请注意,这是特定于Hibernate的,JPA 1.0没有指定二级缓存和查询缓存.
Hibernate遗憾地将sql语句记录到控制台,即使命中来自缓存.
错误,如果使用了缓存,Hibernate不会记录sql语句,使用以下配置进行确认:
persistence.xml中:
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true" />
Run Code Online (Sandbox Code Playgroud)
实体类:
@NamedQuery(name = "byNameLike", query = "SELECT p FROM Person p WHERE name like ?1",
hints = { @QueryHint(name = "org.hibernate.cacheable", value = "true") })
Run Code Online (Sandbox Code Playgroud)
记录:
Enabled org.hibernate.SQL category.
Run Code Online (Sandbox Code Playgroud)