In his excellent blog post "Hibernate query cache considered harmful?" Alex Miller (of Terracotta Inc.) explains why using the query cache can be harmful to latency and scalability.
My question is: is it possible to write a 'get all' DAO method for a particular domain object type that uses the second level cache without having a query cache?
My usual form of code for such a method involves the query cache, e.g.:
public List<Foo> getAllFoo()
{
return (List<Foo>) getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query q = session.createQuery("from Foo");
// Cache the results in the query cache.
q.setCacheable(true);
return q.list();
}
});
}
My only vague thought is maintaining a cached collection of all Foo on some singleton domain object (which is also cached). Is there a more elegant way?