-->
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.  [ 8 posts ] 
Author Message
 Post subject: Sorting a SortedSet
PostPosted: Wed May 25, 2005 9:05 am 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
Hibernate version: 2.1.8

I have a simple 1-N mapping which utilizes a SortedSet. Lets call the owning class A and the class whose instances belong to the SortedSet B. Class B implements Comparable and hence the relation is mapped with sort="natural". Also, class B has a "date" field which is used in the comparison.

Everything works as expected apart from the following use-case:

1. An instance of class A is loaded from the db.

2. The collection of B objects is lazily initialized. At this point the ordering of B's is correct.

3. I change the "date" property of a single element in B in a way that would affect the ordering of the elements in the SortedSet. For the sake of the argument lets say the set includes 5 elements and the affected element was 1st before the change and will end up 5th after it. Nothing has been saved to the db yet!

4. At this point iterating over the elements of the SortedSet produces the previously 1st element still in the 1st place instead of the 5th. Also, set.headSet(affectedElement).size() == 5, ie, all elements of the set (including itself!) are smaller than the affectedElement.

5. If I save the owning entity to the db and retrieve it again the sorted set is in order.

One note: I do use a nonstrict-read-write ehcache for my entities and collections. I tried both not caching and caching with read-write but neither helped.

Anyone knows why this happens? Is these some way I can tell the set to reorder itself?

Many thanks,
Giorgos


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 9:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Read the contracts for java.util.Set and java.util.SortedSet.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 9:29 am 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
Thank you.

Here is the relevant excerpt from java.util.Set for anyone coming across this thread at a later time:

Quote:
Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set.


I was too concerned with reading the javadocs of SortedSet and missed this crucial point in the java.util.Set javadocs.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 5:12 am 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
Hi Giorgos,

thanks for the hint. How did you finally go around this misfeature of Java?

Cheers
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 10:10 am 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
IIRC, I created a new SortedSet and then iterated over the elements of the old SortedSet inserting them one by one to the new one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 2:15 pm 
Newbie

Joined: Mon Aug 29, 2005 1:05 pm
Posts: 3
If you're just modifying the keys of a couple objects in the set, you should remove the object from the set, modify the object and then insert it back into the set. You'll get better performance on large sets that way rather than re-sorting the entire set.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 29, 2005 3:08 pm 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
I remember trying this but it didn't work. Perhaps I don't remember correctly though.


Top
 Profile  
 
 Post subject: Another solution?
PostPosted: Mon Oct 10, 2005 4:18 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hello, is there another solution to this problem?

I can't afford to remove/reinsert all datas in sortedSet as this implies too many useless db modification if i systematically do it.

However, i also can't track modified items to remove/reput them as they are modified by struts servlet directly, as set elements are mapped directly inside the forms (struts retrieve element N of set and modify it's value, which hibernate then maps to the database).

Is there some way to ask hibernate to either reload the set, either reload the bean containing the set? This way it would automatically be in right order after reload.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.