Rajiv,
What you saw makes sense. The query caché just stores a query, its parameters, and the corresponding list of return values.
In order to obtain "hits", you have to use session.get() or session.load()
Generally speaking, caching the queries is not very useful, and even less in a case like yours, where you do SQL changes outside of Hibernate's scope.
Configuration alone, by way of expiration times, won't help you here, if you don't also apply any of the strategies I mentioned before.
As a general rule, caching is only useful when your queries return only keysets (ids) and you get the whole beans from the cache through load() or get().
Code:
public List<WholeBean> getBeans(List<String> ids){
Session session=SessionFactoryUtils.doGetSession(sessionFactory, false);
List<WholeBean> result=new ArrayList<WholeBean>();
for (String id: ids){
result.add((WholeBean)session.load(WholeBean.class, id));
}
return result;
}
This "load" method, which should be called after every "keyset", query will inteligently get things from either the caché or the databae itself, and load it in the caché for further use, you don't have to do anything.
I would complement this strategy by adding some external URL that invokes a "cache clearing" method, to be called manually after every time you do those command-line insertions.
Internally, the only thing that cache-clearing request would do, is to call "evict" on the type of beans you are caching.
Code:
sessionFactory.evict(WholeBean.class);
That takes care of both the session and the underlying cached values.
And no, don't try to use the a caching library outside Hibernate. What for, if these techniques make Hibermate do all the work for you?