Early investigation lead me here:
A wonderful article about hibernate caching. Since our application uses xdoclet for defining hibernate mapping, it wasn't quite complete.
But I really like the analysis because it displays rather clearly how much faster it runs with the caching thrown in.
I'm tempted as well to perform a similar analysis. The article talks about caching the object, and the relationships to the object, which are essential for my problem as well because I have a hierarchy in my objects of Parent -> child. My object is called the Domain Object.
the hbm_xml would be this:
The xdoclet then would be (found here):
/**
* @author petersmiley
*
* @hibernate.class table="Domains" dynamic-insert="true" dynamic-update="true" optimistic-lock="version"
* @hibernate.cache usage="read-only"
* @struts.form "DomainForm"
*
* @auditable.property entity-name="audit.entity.domain"
*/
public class Domain extends BaseObject implements Auditable, Comparable {
.... and the link back to the parent:
/**
* @struts.form-field form-name="parentDomain"
* @hibernate.cache usage="read-write"
* @hibernate.many-to-one
* column="PARENT_ID"
* class="com.norkom.base.resources.model.Domain"
* not-null="false"
* unique="false"
* --cascade="none"
* --outer-join="false""
*
* @auditable.property identified-by="parent.domain"
*/
public Domain getParentDomain() {
return parentDomain;
}
public void setParentDomain(Domain parentDomain) {
this.parentDomain = parentDomain;
}
Note: if you use "read-only" then don't expect to be able to put any objects into the database. When it says read-only, it means it! (I tried it. I had to change to read-write)
Here is my unit test:
public void testCachedCrud() {
List list = domainDao.getAllDomains();
assertEquals(0, list.size());
TestTimer createDomainsTimer = new TestTimer("test Create Domains");
int size = 10;
for (int i=0;i
Domain parentDomain = createBasicDomain(PARENT_DOMAIN_NAME + i);
for(int j=0;j
Domain childDomain = createBasicDomain(CHILD_DOMAIN_DOMAIN + i + ":" + j);
childDomain.setParentDomain(parentDomain);
domainDao.saveDomain(childDomain);
}
domainDao.saveDomain(parentDomain);
}
createDomainsTimer.done();
for (int i=0;i
TestTimer timer = new TestTimer("test Get Domains" + i);
Domain parentDomain = domainDao.getDomainByName(PARENT_DOMAIN_NAME + i);
timer.done();
}
}
Results, with cacheing:
INFO [DomainDaoImplTest] -> Loading config for: /com/norkom/base/business/dao/impl/test-context.xml
EMMA: collecting runtime coverage data ...
WARN [Configurator] -> No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/work/trunk/SUBVERSION/Libs/hibernate/2.1.8/ehcache-0.9.jar!/ehcache-failsafe.xml
WARN [EhCacheProvider] -> Could not find configuration [com.norkom.base.resources.model.Domain]; using defaults.
DEBUG [TestTimer] -> test Create Domains: 140 ms
DEBUG [TestTimer] -> test Get Domains0: 0 ms
DEBUG [TestTimer] -> test Get Domains1: 0 ms
DEBUG [TestTimer] -> test Get Domains2: 0 ms
DEBUG [TestTimer] -> test Get Domains3: 0 ms
DEBUG [TestTimer] -> test Get Domains4: 0 ms
DEBUG [TestTimer] -> test Get Domains5: 0 ms
DEBUG [TestTimer] -> test Get Domains6: 0 ms
DEBUG [TestTimer] -> test Get Domains7: 0 ms
DEBUG [TestTimer] -> test Get Domains8: 0 ms
Now, lets try without caching...
DEBUG [TestTimer] -> test Get Domains9: 0 ms