アプリケーションに Hibernate 3.2.5 を使用しています。実装しようとしていますが、うまくいきQuery Cache
ません。
問題の説明:
初めて、DB がヒットし、データがフェッチされてキャッシュされます。2 回目は、同じクエリに対して、キャッシュからデータを取得するのではなく、DB がヒットします。2 回目は、DB に再度アクセスするのではなく、キャッシュから取得したいと考えています。
を有効にするために、ファイルQuery Cache
に次のエントリを作成しました。cfg.xml
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
ファイルを作成しました: ファイルehcache.xml
に以下のエントリを追加しましたhbm.xml
:
<cache usage="read-only" />
以下は私が試したコードです:
SessionFactory sf = new Configuration().configure("trial.cfg.xml").buildSessionFactory();
Session session = sf.openSession();
List departments1 = session.createQuery("from Dept dept where dept.deptId = 1")
.setCacheable(true)
.setCacheRegion("departmentId")
.list();
//Some business operations
session.flush();
session.close();
//Some business operations
Session session1 = sf.openSession();
List departments2 = session1.createQuery("from Dept dept where dept.deptId = 1").list();
//In the above line again it is hitting the DB rather than taking it from the cache.
キャッシュからデータを取得していないため、DB にヒットしていないものを見逃していると思います。Query Cache
この作業を行う方法を教えてください。