Announcement Announcement Module
No announcement yet.
What is the best way to sort entities? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the best way to sort entities?

    My roo app is mostly used for JSON responses to a thick client request, so the UI for me is really just a "nicety". However, I have a LOT of data, and most of the entities are inter-related with OneToMany and ManyToMany.

    The default UI element used for such relationships is a list. This is fine for the most part since I am pretty much the only "user" of the UI, per say. However, when you have thousands of related objects in the list, and they are all randomly sorted (well, not "random", but sorted by insert instead of the "name" or other text field) it makes it next to impossible to select multiple entries.

    I have searched around, and most seem to say this is a JPA-issue. So, what is the preferred method of sorting data? Is there some magic Roo can do? I saw a JIRA story related to this, but never saw a fix.

    I am guessing first I would need to change all the Set<>s to List<>s. I could put indexes on the desired sorted fields, which would also help for searching, but I am not sure how to do that in a Roo/JPA context outside of a *ToMany relation.

    To extend the question farther, how could one apply sorting to the finders as well, outside of creating a custom finder to so such a task?

    Thanks for the input...

  • #2
    Yes, changing from sets to lists is a good start. JPA also offers @OrderBy annotation which should help.


    • #3
      How does @OrderBy work with the dropdowns for picking a single entry from a list?

      For instance, I have an entity A that references a OneToMany to entity B, which has a inverse ManyToOne relationship back to A, like this:
      public class A {
          String name;
          @OneToMany(cascade = CascadeType.ALL)
          @Fetch(value = FetchMode.JOIN)
          private Set<B> bs = new HashSet<B>();
      public class B {
          String name;
          private A a;
      In the scaffolded code UI, you can only link A->B from the B-side, and you basically get a drop-down list of all As that exist. According to the JavaDoc on @OrderBy, it can be applied to Methods and Fields only, so I can't specify it on the A class. If I put it on the "bs" field in A, its going to sort the Bs, which is not really helpful for the drop-down list of As. And, from what I am reading in the JavaDoc, this only applies to lists, so I can't add it to B.a.

      There should be a way to specify on B which fields to sort A on for the UI element, since its basically doing a "findAllAs". Or, there should be a way on A to specify "this is the default sort order for any UI elements that needs of list of me...".


      • #4
        Ok, thanks for the clarification. In your case it probably makes sense to push in the findAllAs method from the A_Roo_Entity.aj into and simply add an order by statement to the JQL statement.


        • #5
          Thanks, that sounds reasonable. I just wasn't sure if there was some other way to do this with annotations or something.


          • #6
            Anyone interested in Roo supporting such a feature should vote for ROO-241 (and comment on it, if you have something to add).