刚开始学习hibernate,并了解hibernate二级缓存。我创建了一个评论实体,并尝试着它。这是我的代码!
@Entity
@Table(name = "comment")
@FilterDefs(value={@FilterDef(name="projectFilter",parameters=@ParamDef(name="projectID", type="java.lang.Long" )), @FilterDef(name="issueFilter", parameters=@ParamDef( name="issueID", type="java.lang.Long" ) )})
@Filters(value={@Filter(name = "projectFilter", condition = "project_id = :projectID"),
@Filter(name = "issueFilter", condition = "issue_id = :issueID")})
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="comment")
public class Comment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, precision = 5, scale = 0 )
private long id;
@Column(name = "project_id", nullable = false)
private long projectId;
@Column(name = "issue_id", nullable = false)
private long issueId;
@Column(name = "author_id", nullable = false)
private long auhorId;
@Column(name = "author_name", nullable = false)
private String authorName;
@Column(name = "comment")
private String comment;
@Column(name = "created_date")
private Date createdDate;
我想根据项目或项目和问题获取用户评论。在DAO中,我根据项目编写了以下注释函数。
@Autowired
private SessionFactory sessionFactory;
...
@Override
public List<Comment> getAllProjectComments(long projectId) {
Session session = getCurrentSession();
Filter filter = session.enableFilter("projectFilter");
filter.setParameter("projectID", projectId);
return session.createQuery("from Comment").setCacheable(true).list();
}
和ehcahe.xml位于
之下 <?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/hibernate-cache"/>
<defaultCache maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="1200"
timeToLiveSeconds="2400"
overflowToDisk="false"
maxElementsOnDisk="1000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU"/>
<cache name="org.trackMyProject.entity.Comment"
maxElementsInMemory="50"
maxElementsOnDisk="500"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
我在表格中为2个项目添加了500条评论。
当控制器调用DAO方法获取基于项目ID的注释时,后续调用该项目id似乎与二级缓存完美配合,并且DB没有被命中。但是,如果我不断地在2个projectID之间切换,那么每次DB被击中时,我都不想要。
有人可以告诉我我做了什么或者需要做多少配置。
提前致谢!!
EDIT!
类路径:hibernate.cfg.xml中
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
答案 0 :(得分:0)
您正在使用createQuery,这意味着您需要查询级别缓存,请尝试添加以下内容:
<property name='hibernate.cache.use_query_cache'>true</property>