我有以下grails域(示例):
// 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
}
如何检索给定用户当前已 checkout 的按字母顺序排序的书籍列表? (使用条件查询)
一个类似的问题是,如果我没有明确编写Checkout表的代码,而是做了一个has-many:
// a member of the library
class LibraryUser {
string name
static hasMany = [ books : Book ]
}
// a book
class Book {
string title
string author
}
我将如何在此处使用条件查询来检索给定用户当前所有已 checkout 的书的按字母顺序(!)排序的列表?
最佳答案
我认为您需要将关联从Book映射到Checkout,以便您可以执行以下操作:
Book.createCriteria().list{
checkouts{
eq('user', someuser)
}
order('title')
}
关于grails - 使用条件搜索关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1702250/