- This topic has 9 replies, 2 voices, and was last updated 13 years, 11 months ago by
jkennedy.
-
AuthorPosts
-
Greg SoulsbyMemberI 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
jkennedyMemberThere 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
Greg SoulsbyMemberJack,
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 LinkedHashSetFor 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.
jkennedyMemberSorry, 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
Greg SoulsbyMemberJack,
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 LinkedHashSetSeems 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
Greg SoulsbyMemberok 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.
jkennedyMemberI 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
Greg SoulsbyMemberWhen 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.
Greg SoulsbyMemberok, 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.
jkennedyMemberThanks for posting this information, it may be useful to others who are interested in making the same types of changes.
We appreciate it.
jack
-
AuthorPosts