Search code examples
hibernatecachingsecond-level-cache

Is the second-level cache support for caching the query result exclude the entity


Normal query manner:

Session sess=factory.getCurrentSession();
sess.beginTran....();

String hql="from Entity en where en......";
Query q=sess.createQuery(hql).setCacheble(true);
List<Entity> list=q.list();

In this case,the object in the list are "entity"s. They will be cached.

However I wonder if cache would work if the hql is like this:

hql="select new Result(count(xx),sum(xxx)) from Entity en ....";

class Result{
  long num;
  long sum;
  //constructor,getter and setter
}

Now,the type of the objects in the result list will be Result.

List<Result> list=q.list();

My question is if the Result Object can be saved at the second-level cache?

Also how about the ResultTransformer?

q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

At this case,the type of the objects in the q.list() will be Map,does these maps can be cached also?


Solution

  • It certainly works for "new Result(...)" but I'm not 100% sure about the ResultTransformer. I cannot see a reason why it wouldn't, considering that the transformation is made after the query.