Searching association table using criteria
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 Che开发者_运维技巧ckout {
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?
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')
}
精彩评论