Grails

Add default sort and order by property to domain classes

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.3.1
  • Fix Version/s: 1.1-beta1
  • Component/s: Persistence
  • Labels:
    None

Description

I would be nice if you could specify a default sort and order for domain classes, like:

class NewsArticle { static defaultSort = 'publishedAt' static defaultOrder = 'desc' String title Date publishedAt .... }

Issue Links

Activity

Hide
David Smiley added a comment -

This is an excellent idea Paul. I was just about to submit an issue and saw that you did for me. I have a large domain model and I find it annoying to have to always mention what I want to sort something by. I'd like to be able to declare in my model what the default is, and then I'll get automatic sorting with the list and findAllBy* methods without having to further specify things. Most if not all times you query for a list of things, you're going to show it to the user so I hold the opinion that you should get a consistent order in the default case (without having to specify anything). I think the id (which would amount to the insertion order) should be the default. This means you'll always get a sort.

Show
David Smiley added a comment - This is an excellent idea Paul. I was just about to submit an issue and saw that you did for me. I have a large domain model and I find it annoying to have to always mention what I want to sort something by. I'd like to be able to declare in my model what the default is, and then I'll get automatic sorting with the list and findAllBy* methods without having to further specify things. Most if not all times you query for a list of things, you're going to show it to the user so I hold the opinion that you should get a consistent order in the default case (without having to specify anything). I think the id (which would amount to the insertion order) should be the default. This means you'll always get a sort.
Hide
David Smiley added a comment -

By the way, this feature should also apply to hasMany on the domain – they should also benefit automatically from the user-supplied sort order.

Show
David Smiley added a comment - By the way, this feature should also apply to hasMany on the domain – they should also benefit automatically from the user-supplied sort order.
Hide
Graeme Rocher added a comment -

Can now be achieved with the following code:

class Book {
    String title

    static belongsTo = [author:Author]

    static mapping = {
        sort title:'desc'
    }

}
class Author {
    String name
    static hasMany = [books:Book]

    static mapping = {
        sort 'name'
        books sort:'title'
    }
}
Show
Graeme Rocher added a comment - Can now be achieved with the following code:
class Book {
    String title

    static belongsTo = [author:Author]

    static mapping = {
        sort title:'desc'
    }

}
class Author {
    String name
    static hasMany = [books:Book]

    static mapping = {
        sort 'name'
        books sort:'title'
    }
}
Hide
Hubert Chang added a comment -

I tested it but it is not functioned yet. I tested this with grails-1.1-snapshot(GRAILS-GRAILS11JDK15-343).

Show
Hubert Chang added a comment - I tested it but it is not functioned yet. I tested this with grails-1.1-snapshot(GRAILS-GRAILS11JDK15-343).
Hide
Graeme Rocher added a comment -

We have tests that cover this behavior, if you have an example that is not working please post some code that shows it is not working

Show
Graeme Rocher added a comment - We have tests that cover this behavior, if you have an example that is not working please post some code that shows it is not working
Hide
Dennis Carroll added a comment -

All my attempts to implement Graeme's suggestion have not succeeded.
I will try to attach the code (w/test) I used.

Show
Dennis Carroll added a comment - All my attempts to implement Graeme's suggestion have not succeeded. I will try to attach the code (w/test) I used.
Hide
Josh Brown added a comment -

I found that this doesn't work when the property is a camelCased word. For example:

class Book {
Date dateCreated
}

class Author {
static hasMany = [books:Book]

static mapping = { books sort:'dateCreated' }
}

Running the app produces an exception similar to the following:

ERROR util.JDBCExceptionReporter - Column not found: BOOKS0_.DATECREATED in statement ...

The problem is that the database column is date_created, not datecreated. The camelHumps property should be translated in the query to two words separated by an underscore.

Show
Josh Brown added a comment - I found that this doesn't work when the property is a camelCased word. For example: class Book { Date dateCreated } class Author { static hasMany = [books:Book] static mapping = { books sort:'dateCreated' } } Running the app produces an exception similar to the following: ERROR util.JDBCExceptionReporter - Column not found: BOOKS0_.DATECREATED in statement ... The problem is that the database column is date_created, not datecreated. The camelHumps property should be translated in the query to two words separated by an underscore.
Hide
Justin Edelson added a comment -

Did the issue that Josh reported above get resolved? I'm seeing this same problem in 1.2.1.

Show
Justin Edelson added a comment - Did the issue that Josh reported above get resolved? I'm seeing this same problem in 1.2.1.
Hide
Tomas Metz added a comment -

The problem persists. I have just upgraded from 1.1.2 to 1.3.1 and the application even did not started. I have an attribute in the domain 'int itemOrder', using for default sort and for the 1.3.1 it was necessary to change: name 'itemOrder' -> name 'item_order'.

Show
Tomas Metz added a comment - The problem persists. I have just upgraded from 1.1.2 to 1.3.1 and the application even did not started. I have an attribute in the domain 'int itemOrder', using for default sort and for the 1.3.1 it was necessary to change: name 'itemOrder' -> name 'item_order'.
Hide
Alexander Zeillinger added a comment -

Broken in 1.3.7 when sorting dates, order is always ascending.

Show
Alexander Zeillinger added a comment - Broken in 1.3.7 when sorting dates, order is always ascending.

People

Vote (10)
Watch (9)

Dates

  • Created:
    Updated:
    Resolved: