-->
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.  [ 3 posts ] 
Author Message
 Post subject: Sort problems after upgrade to Hibernate3
PostPosted: Thu May 18, 2006 5:04 pm 
Beginner
Beginner

Joined: Fri Feb 24, 2006 1:18 pm
Posts: 25
I am in the process of upgrading my application to Hibernate3 and things are going well with one exception. One of my sets is sorting in the wrong order.

Here is the relevant .hbm snippet:
Code:
<set
  cascade="all-delete-orphan"
  name="ChildInCampWeekSet"
  table="ChildInCampWeek"
  sort="camp.ChildInCampWeek$ChildInCampWeekComparator"
  inverse="true"
>
<key column="ChildId" />
  <one-to-many class="ChildInCampWeek" />
</set>


As you can see, there is a comparator to do the sorting. However, I notice that this comparator is not being called. For example, I have some test code:
Code:
    Child child = DBLayer.loadChild(childId);
    Set<ChildInCampWeek> childInCampWeekSet = child.getChildInCampWeekSet();
    for (ChildInCampWeek ciCW : childInCampWeekSet) {
      System.out.println("Start date is: " + ciCW.getCampWeek().getWeek().getStartDate().toString());
    }

The output shows that the start dates are sorted from last to first, not first to last. My initial thought was that somehow my comparator worked fine on Hibernate 2.1, but somehow there was a subtle bug that caused my comparator to fail in Hibernate 3. But instrumenting the comparator seems to indicate that it is not even being called.

I have tried modifying the Set above to be a SortedSet, but no change (and that required me to modify the code that HibernateSynchonizer generated).

I have also looked at the generated SQL and I do not see any order by clauses being generated.

Any ideas as to what I may have stumbled into are greatly appreciated!

Thanks SO much!
RB


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 5:41 pm 
Beginner
Beginner

Joined: Fri Feb 24, 2006 1:18 pm
Posts: 25
Another clue that the problem is that the Comparator is not being called (vs. the comparator being wrong). I inverted the logic in the Comparator, which should have reversed the sorting - but there was no difference. Of course, the fact that the println statements that I have in the Comparator don't print out anything is the real evidence that the Comparator is not being called).

Now why would the Comparator not be called if the .hbm file clearly specifies a sort. (I am sure that there is a logical explanation for all this).

TIA,
RB


Top
 Profile  
 
 Post subject: Resolution - but a mystery remains...
PostPosted: Fri May 19, 2006 12:25 pm 
Beginner
Beginner

Joined: Fri Feb 24, 2006 1:18 pm
Posts: 25
In further reading and experimenting, I was able to work around my problem - but on the surface, there does appear to be some change in Hibernate3.

In particular, I was able to get sorting to work by implementing the compareTo method in my ChildInCampWeek class (the class that also has the ChildInCampWeekComparator inner class).

I ran my test and saw that the compareTo method was the method that was being called to do the sorting. It appeared that the:
Code:
sort="camp.ChildInCampWeek$ChildInCampWeekComparator"

was being ignored. And, now that I had a compareTo method, sorting worked fine.

I went and changed my Child.hbm to use:
Code:
sort="natural"

and everything continued to work fine.

Upon reflection, it turns out that the right (and easier) way for me to do things is to have the compareTo and use sort="natural", but the mystery remains as to why sorting using the Comparator stopped working when I upgraded to Hibernate3 (and the associated HibernateSynchronizer plugin).

Anyone have any ideas? (I hate having unresolved myteries :) ).

Thanks!!
RB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.