-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: query cache always returns result with lazy collections
PostPosted: Sun Aug 02, 2009 1:19 am 
Newbie

Joined: Sat May 30, 2009 2:47 am
Posts: 12
I am trying to give second level cache and query cache a try for my project. here is the versio information:

1. Hibernate: 3.3.2
2. EHCache: 1.6.0

However, although query cache seems to be working. It always return result with lazy collections, even if my original query eagerly fetch the collections. For example:

In Service class
// open session
criteria = session.createCriteria(Podcast.class);
criteria.setFirstResult(offset);
org.hibernate.Criteria criteria = session.createCriteria(Podcast.class);
criteria.setFetchMode("podcastCategoryMappings", FetchMode.JOIN);
criteria.add(Restrictions.in("id", podIds));
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
criteria.addOrder(Order.asc("title"));
criteria.setCacheable(true); // turn on cache
podcasts = criteria.list();
// close session

In client class, I expect getting a podcast class, with podcastCategoryMappings collection fetched.

@Test
public void testGetAllPodcasts() {
// first time, it goes through database, and put into cache the query result (id=1), entity (podcast), collections(podcastCategoryMapppings)
List<Podcast> podcasts = _service.getPodcasts(0, 1);

for (Podcast pod: podcasts) {

Set<PodcastCategoryMapping> mappings = pod.getPodcastCategoryMappings();
for (PodcastCategoryMapping mapping: mappings) {
System.out.println("pod title/id: " + pod.getTitle() + "/" + pod.getId() + " category id: " + mapping.getCategoryId());
}


// second time, the results come from cache. I shall expect cache hit for query result, entity(podcast), and collections.
// However, i only see cache hit for query result and entity, not the collections.
podcasts = _service.getPodcasts(0, 1);

// It will fail with LazyInitializationException since collection is not fully initialized when result returned from cache
for (Podcast pod: podcasts) {
System.out.println("pod id: " + pod.getId());

Set<PodcastCategoryMapping> mappings = pod.getPodcastCategoryMappings();
for (PodcastCategoryMapping mapping: mappings) {
System.out.println("pod title/id: " + pod.getTitle() + "/" + pod.getId() + " category id: " + mapping.getCategoryId());
}

}
}


I turned on the debug message for hibernate cache related classes, so that I know what is wrong with cache.
log4j.logger.org.hibernate.cache.ReadWriteCache=TRACE
log4j.logger.org.hibernate.cache.UpdateTimestampsCache=TRACE
log4j.logger.org.hibernate.cache.StandardQueryCache=TRACE


Can someone from hibernate team take a look at this and confirm if this is a bug? I am very close to place this into a project. If this is not working, I guess, I have to manage cache myself, rather than using hibernate to manage the cache. The bottom line is that cache should not return inconsistent result like this.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.