Results 1 to 6 of 6

Thread: sort order and finders

  1. #1
    Join Date
    Sep 2009
    Posts
    101

    Default sort order and finders

    I can use an @orderby annotation on a @OneToMany in my domain object like this (e.g., Employee.java):

    Code:
        
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "employee")
        @OrderBy("date DESC")
        private List<Task> tasks = new ArrayList<Task>();
    But is there a strategy for setting a sort order on the queries returned by finders (besides pushing in the aspect and adding ORDER BY to the query)?

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    It seems a reasonable enhancement suggestion to improve the dynamic finders to allow an order by to be specified when creating the dynamic finder.

    One issue is whether to incorporate this into the finder name (eg findPeopleByNamesOrderBySurname) or provide an alternate way of specifying it in the @RooEntity annotation (eg @RooEntity(finders="findPeopleByNames/surname,firstName") which uses a slash as the delimiter to indicate an ordered, comma-separated list of sort orders). I'm just throwing around ideas here to get a conversation started; I don't have a considered view on this issue.

    If you'd like to log a Jira ticket at http://jira.springframework.org/browse/ROO we can explore this a little more there.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  3. #3
    Join Date
    Sep 2009
    Posts
    101

    Default

    Added to tracker: http://jira.springframework.org/browse/ROO-241

    I'd be happy enough with either solution you suggest. Using the finder name would be more flexible (since a user could create a new finder with a different order clause), but a "default" case would handle most of the use cases I've come across.

    Even more flexible would be to allow the 'order by" clause to be passed in to the finder method (perhaps via some kind of "OrderClause" object). But I suspect that may not be desirable on your end.

    Btw, there are a couple of related enhancements that would give developers more control over sort order in queries.

    1) Allow the use of "lists" (instead of sets) when creating new fields on objects
    2) Add optional "-OrderBy" on the list generated in #1

    I.e., I want to do this:

    Code:
    add field list -class ~.domain.Employee -fieldName tasks -element ~.domain.Task -mappedBy employee -orderBy ~.domain.Task.description
    to get this:

    Code:
    public class Vet
    {
         ...
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "employee")
        @javax.persistence.OrderBy("description ASC")
        private List<Task> specialties = new ArrayList<Task>();
    }
    (I'd be happy to add these to the issue or put in a separate feature request if you think these are appropriate ideas.)

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Thanks for the Jira report and further suggestions.

    Passing in an order clause is an interesting idea. Indeed we could make it accept a var-arg that represents zero or more order by clauses, and introduce some static fields to the entity which represents available order by clauses.

    Historically Stefan and I considered an advanced builder-style generator for making dynamic finders considerably more flexible. It might prove more desirable to tackle this use case via that technique, rather than getting too complicated with the relatively simple existing dynamic finders.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  5. #5
    Join Date
    Feb 2010
    Posts
    3

    Default

    Quote Originally Posted by mikej View Post
    I'd be happy enough with either solution you suggest. Using the finder name would be more flexible (since a user could create a new finder with a different order clause), but a "default" case would handle most of the use cases I've come across.
    I would like to second the addition of a @RooEntity parameter for an entity's default sort field. I haven't noticed any particular sort order when hooking up a pre-existing Accounts table, which hurts the usability of the built-in list action.

  6. #6
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Don't forget to vote for ROO-241 if you're interested in this. Also please feel free to comment in the issue tracker on ROO-241 if you have implementation ideas or use cases.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •