tags:

views:

34

answers:

1

I have the following grails domains (example):

// a member of the library
class LibraryUser {
    string name
}

// a book
class Book {
    string title
    string author
}

// association which user currently has which book checked out
class Checkout {
     LibraryUser user
     Book        book
}

How can I retrieve an alphabetically sorted list of books that a given user has currently checked out? (using a criteria query)

A similar question is if I do not explicitely code the Checkout table but instead do a has-many:

// a member of the library
class LibraryUser {
    string name

    static hasMany = [ books : Book ]
}

// a book
class Book {
    string title
    string author
}

How would I use a criteria query here to retrieve an alphabetically(!) sorted list of all currently checked out books for a given user?

A: 

I think you'll need to map the association from Book to Checkout so that you can do:

Book.createCriteria().list{
    checkouts{
        eq('user', someuser)
    }
    order('title')
}
leebutts
Yes, this would work. However for other reasons (e.g. question 1702369 and 1706189) the mapping is currently not working for me.
Steve