facebook

Sorting result sets

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #314858 Reply

    Greg Soulsby
    Member

    I am getting a Set of children back from the parent, no problem.

    You cant sort a set, I understand. Is there a way to change the code so the set is returned sorted? That would be good because they have a specific sequnce.

    Or if I need to sort the returned set, what are the options for doing that?

    Thanks

    #314901 Reply

    jkennedy
    Member

    There are a few things to review in this area, and your approach will depend on your needs.

    I am assuming that the code that you are working with was generated using the MyEclipse for Spring Scaffolding option and that you are referring to the generated Relationships in a JPA entity that are implemented as a Set.

    If you are using Hibernate as your persistence provider (default), you can change the generated code to return SortedSet instead of Set and change the generated code to use TreeSet instead of LinkedHashSet.

    You must add the Hibernate annotation @Sorted to the relationship declaration, and using this annotation you can specify a custom Comparator to control the order, or use a default implementation: See http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html
    Generally looks something like this: @Sort(type = SortType.COMPARATOR, comparator = MyComparator.class)

    Hope this helps,
    Jack

    #314968 Reply

    Greg Soulsby
    Member

    Jack,

    Your assumptions are correct. And yes, helps a lot. And you give me over confidence – meaning I will try this:

    1) on ChildClass add “implements comparator”
    2) to ChildClass add the function
    public int compare(ChildClass element1, ChildClass element2)
    { return element1.getField().compareTo(element1.getField());}
    3) where I find the @OneToMany for this relationship add
    @Sort(type = SortType.COMPARATOR, comparator = ChildClass.class)
    4) change the generated code to return SortedSet instead of Set
    5) change the generated code to use TreeSet instead of LinkedHashSet

    For 4) and 5) I am not sure where that generated code is, but I will look. Perhaps an error will occure in these places when I try to run it.

    Many thanks.

    #314984 Reply

    jkennedy
    Member

    Sorry, the code I was referring to is in the JPA entity, you will see it as soon as you make your changes since it will result in a compile error.

    Let me know how this works for you.

    Thanks,
    Jack

    #315082 Reply

    Greg Soulsby
    Member

    Jack,

    I made the changes, except for

    4) change the generated code to return SortedSet instead of Set
    5) change the generated code to use TreeSet instead of LinkedHashSet

    Seems to compile “As Is” fine, and does not output an error when run. Code seems to be returning one rec only.

    So can you give me the name of the file which houses the JPA entity?

    Greg

    #315088 Reply

    Greg Soulsby
    Member

    ok Jack, worked out the approach – where you get the child set, make the type of the returned set SortedSet (not TreeSet as as the annotation processing fails- only took 2 hours to work that out). Then the compile errors show in the called code and you work through them, changing Set to SortedSet and LinkedHashSet to TreeSet.

    On the scafolded pages, where the list of child records is listed below the parent, I am now seeing only one row, when there are actually many. Will look at that later.

    #315205 Reply

    jkennedy
    Member

    I should have been more clear perhaps in the post above.

    Where I said to “return” SortedSet, I was saying that the method should have SortedSet for a return type.

    When I say change the generated code to use TreeSet instead of LinkedHashSet, I meant to find the places in the code where LinkedHashSet was being used as the “implementation” of the set, and change that implementation to be TreeSet.

    Let me know if you can’t resolve the issue.

    Are there any errors in the console view for your running application server?

    Thanks,
    Jack

    #315304 Reply

    Greg Soulsby
    Member

    When I implement this it is returning a set of size 1. No errors on console.

    Have spend hours trying to get it to work.

    The things I know include the fact that the sort is happening, as I am printing something within the compare function to prove that. So how can I trap the step that the records get lost after this?

    In searching for a clue, I have a question. The child record is a many to many breaker, where the relationship is between records on the same table, what I am calling the parent. The child has 2 foreign key fields which point to the same unique id field on the parent table. Would that cause something to break? For example, do I need to implement the @Sort(type = SortType.COMPARATOR, comparator = ChildClass.class) on both the one to many relationships that that field has? Something else?

    All highly unlikely I know, but desparate times call for deparate questions.

    #315312 Reply

    Greg Soulsby
    Member

    ok, working now. Dont know why. One thing I learnt was that you can add one to a sortedset that does not have a comparator, but it crashes if you try to add again, which makes sense. So at some stage I must have been trying to add into a sorted set that did not have a comparator. Would expect that to cause a consol message but I saw nothing.

    #315373 Reply

    jkennedy
    Member

    Thanks for posting this information, it may be useful to others who are interested in making the same types of changes.

    We appreciate it.

    jack

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: Sorting result sets

You must be logged in to post in the forum log in