grails - 使用条件搜索关联表

标签 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 的按字母顺序排序的书籍列表? (使用条件查询)

一个类似的问题是,如果我没有明确编写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/

相关文章:

grails - GORM 陷阱第 2 部分 作者:Peter Ledbrook

tomcat - 几分钟后 grails 上的 Atmosphere 挂起

html - 发送到客户端之前,请在Grails 3中修改响应的呈现字符串。

java - 展开所有实例的抽象 Java 类

grails - 在一个字段grails域上应用AND条件

grails - grails 3-获取子域对象

unit-testing - Grails Interceptor模型在单元测试中为空

grails - grails形式的可选控件

grails - 具有索引属性的hasErrors

grails - 是否可以通过Webflow Controller操作访问 View 状态?