-->
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.  [ 2 posts ] 
Author Message
 Post subject: Query cache not updated if many2many (n:m) relation modified
PostPosted: Fri May 18, 2012 7:26 am 
Newbie

Joined: Fri May 18, 2012 6:29 am
Posts: 2
Hi.
I'm using Hibernate 3.6.10 Final with second level cache and query cache (ehcache 2.4.7).
The web application is mostly reading data and has only few updates, so the cache could really improve performance and I would like to use it.

In most cases the caches work as expected. But there is one case which seems to be quite strange.

We have two entities person and group which form a many2many relation.

The HTML gui shows a list with all persons belonging to a selected group. The data is selected via criteria query. So far ok.
Each line in the list contains an remove-link to remove a person from the group.
The code behind is simple:
person.getGroups().remove(group);
group.getPersons().remove(person);
this.personHome.attachDirty(person);

This works so far as the record in the mapping table is removed from database. Both entities certainly are not removed.

But after the deleting, the gui should again show the list of persons in the selected group.
And now in the list there is still shown the person which was deleted from the group. Arghh! Why???

If I disable the query cache it works fine.
If I add a senseless person.setComment("test" + new Date()) it works fine.

So it look like the removing on both sides does not affect the query cache. The query cache is invalidated only if the person object itself is changed.

How can I get the query cache invalidated even on changes of the many2many relation?

Thanks for any help,

Horst


Top
 Profile  
 
 Post subject: Re: Query cache not updated if many2many (n:m) relation modified
PostPosted: Mon May 21, 2012 3:20 am 
Newbie

Joined: Fri May 18, 2012 6:29 am
Posts: 2
Finally I found the solution:
The compareTo method of the Group entity was implemented wrong an did not return 0 for equal objects. And because the person.groups is a SortedSet, it relies on the compareTo() method to find the matching entry.

So the remove() did not really remove an element, because it did not find the element to be removed. Consequence: No change an person instance, no cache invalidation.

After fixing the compareTo, everything works as expected, even with the code example in my first post.


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

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.