Working with Cached Associations

Suppose you need to display the list of employees (with employee names, languages spoken, etc.) for a given country. The following is the Hibernate mapping of the Employee class:

<hibernate-mapping package="com.wakaleo.articles.caching.businessobjects">
<class name="Employee" table="EMPLOYEE" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<id name="id" type="long" unsaved-value="null" >
<column name="emp_id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="emp_surname" name="surname" type="string"/>
<property column="emp_firstname" name="firstname" type="string"/>
<many-to-one name="country"
column="cn_id"
class="com.wakaleo.articles.caching.businessobjects.Country"
not-null="true" />
<!-- Lazy-loading is deactivated to demonstrate caching behavior -->
<set name="languages" table="EMPLOYEE_SPEAKS_LANGUAGE" lazy="false">
<key column="emp_id"/>
<many-to-many column="lan_id" class="Language"/>
</set>
</class>
</hibernate-mapping>

Suppose you really need to load the languages spoken by an employee every time you use the Employee object. To force Hibernate to automatically load the languages set, you set the lazy attribute to false. (This is just for the sake of this example. In general, deactivating lazy loading is not a good idea. Do it only when absolutely necessary.) You will also need a DAO class to fetch the employees. The following one would do the trick:


public class EmployeeDAO {
public List getEmployeesByCountry(Country country) {
return SessionManager.currentSession()
.createQuery(
"from Employee as e where e.country = :country "
+ " order by e.surname, e.firstname")
.setParameter("country",country)
.list();
}
}

Next, write some simple unit tests to see how it performs. As in the previous example, you should see how it performs when called repeatedly:


public class EmployeeDAOTest extends TestCase {
CountryDAO countryDao = new CountryDAO();
EmployeeDAO employeeDao = new EmployeeDAO();
/**
* Ensure that the Hibernate session is available
* to avoid the Hibernate initialisation interfering with
* the benchmarks
*/
protected void setUp() throws Exception {
super.setUp();
SessionManager.getSession();
}
public void testGetNZEmployees() {
TestTimer timer = new TestTimer("testGetNZEmployees");
Transaction tx = SessionManager.getSession().beginTransaction();
Country nz = countryDao.findCountryByCode("nz");
List kiwis = employeeDao.getEmployeesByCountry(nz);
tx.commit();
SessionManager.closeSession();
timer.done();
}
public void testGetAUEmployees() {
TestTimer timer = new TestTimer("testGetAUEmployees");
Transaction tx = SessionManager.getSession().beginTransaction();
Country au = countryDao.findCountryByCode("au");
List aussis = employeeDao.getEmployeesByCountry(au);
tx.commit();
SessionManager.closeSession();
timer.done();
}
public void testRepeatedGetEmployees() {
testGetNZEmployees();
testGetAUEmployees();
testGetNZEmployees();
testGetAUEmployees();
}
}

If you run a test using the above configuration, you should get something like the following:


$mvn test -Dtest=EmployeeDAOTest
...
testGetNZEmployees: 1227 ms.
testGetAUEmployees: 883 ms.
testGetNZEmployees: 907 ms.
testGetAUEmployees: 873 ms.
testGetNZEmployees: 987 ms.
testGetAUEmployees: 916 ms.
[surefire] Running com.wakaleo.articles.caching.dao.EmployeeDAOTest
[surefire] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 3,684 sec

So loading the 50 or so employees assigned to each country takes about a second each time. That's way too slow. This is typical of the N+1 query problem. If you activate the SQL logs, you will see one query on the EMPLOYEE table, followed by literally hundreds of queries on the LANGUAGE table: whenever Hibernate retrieves an employee object from the cache, it reloads all the associated languages. So how can you improve on this? The first thing to do is activate read/write caching on the Employee class as follows:


<hibernate-mapping package="com.wakaleo.articles.caching.businessobjects">
<class name="Employee" table="EMPLOYEE" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<cache usage="read-write"/>
...
</class>
</hibernate-mapping>

You should also activate caching on the Language class. Read-only caching should do here:


<class name="Language" table="SPOKEN_LANGUAGE" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<cache usage="read-only"/>
...
</class>
</hibernate-mapping>

Then, you will need to configure the cache rules by adding the following entries to the ehcache.xml file:


<cache name="com.wakaleo.articles.caching.businessobjects.Employee"
maxElementsInMemory="5000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
/>
<cache name="com.wakaleo.articles.caching.businessobjects.Language"
maxElementsInMemory="100"
eternal="true"
overflowToDisk="false"
/>

This is fine, but it doesn't solve the N+1 query problem: 50 or so extra queries will still be executed whenever you load an Employee. This is a case where you need to activate caching on the language association in the Employee.hbm.xml mapping file, as follows:


<hibernate-mapping package="com.wakaleo.articles.caching.businessobjects">
<class name="Employee" table="EMPLOYEE" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<id name="id" type="long" unsaved-value="null" >
<column name="emp_id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="emp_surname" name="surname" type="string"/>
<property column="emp_firstname" name="firstname" type="string"/>
<many-to-one name="country"
column="cn_id"
class="com.wakaleo.articles.caching.businessobjects.Country"
not-null="true" />
<!-- Lazy-loading is deactivated to demonstrate caching behavior -->
<set name="languages" table="EMPLOYEE_SPEAKS_LANGUAGE" lazy="false">
<cache usage="read-write"/>
<key column="emp_id"/>
<many-to-many column="lan_id" class="Language"/>
</set>
</class>
</hibernate-mapping>

In this configuration, you should get near-optimal performance:


$mvn test -Dtest=EmployeeDAOTest
...
testGetNZEmployees: 1477 ms.
testGetAUEmployees: 940 ms.
testGetNZEmployees: 65 ms.
testGetAUEmployees: 65 ms.
testGetNZEmployees: 76 ms.
testGetAUEmployees: 52 ms.
[surefire] Running com.wakaleo.articles.caching.dao.EmployeeDAOTest
[surefire] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0,228 sec