2014-02-16 163 views
0

剛開始學習hibernate,並瞭解休眠二級緩存。我創建了一個評論實體並嘗試着去實現它。這是我的代碼!休眠二級緩存瞭解

@Entity 
@Table(name = "comment") 
@FilterDefs(value={@FilterDef(name="projectFilter",[email protected](name="projectID", type="java.lang.Long")), @FilterDef(name="issueFilter", [email protected](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中,我寫了以下基於projectid的評論函數。

@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" 
    /> 

我已經添加在表500個徵求意見2個projectids。

當控制器調用DAO方法獲取基於項目ID的註釋以及隨後對該項目ID的調用似乎與二級緩存完美協作並且數據庫沒有被擊中時。但是如果我不斷地在兩個projectID之間切換,那麼每次DB被擊中時,我都不想要。

有人可以告訴我我做了什麼樣的錯誤或需要做更多的配置。

在此先感謝!

編輯!

類路徑: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

可能重複[什麼是休眠的第一和第二級緩存?](http://stackoverflow.com/questions/337072/what-is-first-and-second-level-caching-in-hibernate) – NimChimpsky

回答

0

您正在使用的createQuery這將意味着你需要查詢級緩存,嘗試添加這樣的:

<property name='hibernate.cache.use_query_cache'>true</property> 
+0

已經做了befo回覆!在編輯部分添加了休眠屬性! – Debapriya