使用条件搜索关联表

时间:2009-11-09 16:44:43

标签: grails

我有以下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表,而是做一个has-many:

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

    static hasMany = [ books : Book ]
}

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

我如何在此处使用条件查询来检索给定用户的所有当前已检出书籍的按字母顺序排列的列表(!)?

1 个答案:

答案 0 :(得分:1)

我认为您需要将关联从Book映射到Checkout,以便您可以执行以下操作:

Book.createCriteria().list{
    checkouts{
        eq('user', someuser)
    }
    order('title')
}
相关问题