I've been struggling for nearly a day to enable hibernate caching with either OSCache and Ehcache. The caching providers are configured correctly since no exceptions are thrown. My query executes and returns the correct result. The problem is that the query always executes, but I want it to retrieve the result from the cache.
Here is what I have (I've tried not using a session-factory name as well as the EHcache provider):
Code:
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
name="connection.datasource">a data source</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.ResinTransactionManagerLookup</property>
<mapping resource="com/testdesigner/image/ImageHash.hbm.xml"/>
</session-factory>
Mapping file (I've tried read-only):
Code:
<hibernate-mapping>
<class name="com.testdesigner.image.ImageHash" table="ImageHashes">
<cache usage="read-write"/>
<id name="url" column="imageURL">
<generator class="assigned"/>
</id>
<property name="hashCode" column="imageHashCode"/>
</class>
</hibernate-mapping>
My Java code:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from ImageHash where url = :url");
query.setString("url", url.toString());
ImageHash imageHash = (ImageHash) query.uniqueResult();
tx.commit();
} finally {
if (session.isOpen()) session.close();
}
My log continues to show the hibernate query for each http request.
Code:
Hibernate: select imagehash0_.imageURL as imageURL1_, imagehash0_.imageHashCode as imageHas2_1_ from ImageHashes imagehash0_ where imagehash0_.imageURL=?
Any suggestions on what I should try. Also, I have both ehcache.xml and oscache.properties files in my classpath. I'm using the most recent version of Hibernate core, OSCache and EHCache. They are using the a simple configuration, but I have tried several settings without improvement.
My goal is to cache the objects returned from this query.
Thank you for your help.
Michael