开发者

Hibernate EntityManager + Query Cache - "join fetch" not working

开发者 https://www.devze.com 2023-03-01 11:02 出处:网络
I am trying to cache a query like this: TypedQuery<Foo> q = em.createQuery( \"SELECT foo FROM Foo foo \" +

I am trying to cache a query like this:

TypedQuery<Foo> q = em.createQuery(
    "SELECT foo FROM Foo foo " +
    "INNER JOIN FETCH Foo.bar "
);
q.setHint("org.hibernate.cacheable", true);

The problem is that the FETCH doesn't seem to have any effect when the cache is hit, i.e. the collection Foo.bar doesn't get initialized. I could iterate over the the result list and initialize all instances of the collection by hand, but that would make the whole thing even slower than without using the query cache in the first place.

I use JBoss AS 6.0 / Hibernate 3.6 with Infinispan as cache engine.

Curiously, ac开发者_如何学JAVAcording to the cache statistics I get through the JMX console, objects in Foo.bar seem to get cached, but the cache for those objects doesn't get any hits.


It can be fixed by applying @Cache to the collection property (of course, target entity should be cacheable too), see 21.2.1. Cache mappings.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号