在 Grails 中,如何订购急切获取的域记录?

发布于 2024-11-05 22:55:39 字数 770 浏览 4 评论 0原文

您好,我想订购另一个域类以 1:m 关系拥有的急切获取的域对象,但不知道如何执行此操作。当我尝试使用简化的项目时,出现错误。以下是我的尝试:

class Picture {
    String name

    static hasMany = [comments:Comment]
    static mapping = {
        comments(lazy:false, sort:'content', order:'desc')
    }
}

class Comment {
    String content
    Date dateCreated

    static belongsTo = [Picture]
}

现在,当测试如何使用 println Picture.get(1) 作为 JSON 获取记录时,我收到以下错误:

java.sql.SQLException: 未找到列:语句中的 COMMENTS0_.CONTENT [从 picture_comment comments0_ 中选择 comments0_.picture_comments_id 作为 picture1_0_, comments0_.comment_id 作为 comment2_0_ 其中 comments0_.picture_comments_id=? order by comments0_.content desc

如果没有sort:'content', order:'desc',评论将按随机顺序排列,但不会出错。

Greetings, I would like to order eagerly fetched domain objects that another domain class owns in a 1:m relationship, but cannot figure how to do this. When I try with a simplified project, I get an error. Below are my attempts:

class Picture {
    String name

    static hasMany = [comments:Comment]
    static mapping = {
        comments(lazy:false, sort:'content', order:'desc')
    }
}

class Comment {
    String content
    Date dateCreated

    static belongsTo = [Picture]
}

Now, when testing how the record fetch with println Picture.get(1) as JSON, I get the following error:

java.sql.SQLException: Column not found: COMMENTS0_.CONTENT in statement [select comments0_.picture_comments_id as picture1_0_, comments0_.comment_id as comment2_0_ from picture_comment comments0_ where comments0_.picture_comments_id=? order by comments0_.content desc

Without sort:'content', order:'desc', the Comments are in random order but no error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-11-12 22:55:39

我最终在获取了孩子列表后进行了排序,当时我在代码中需要它们。像这样的东西:

def sortedComments = myPicture.comments.sort{it.content}.reverse()

具有允许根据需要进行无限排序配置的优点,而不是默认情况下仅检索一个。

I ended up doing the ordering after the list of children was already fetched, at the time I need them in the code. Something like this:

def sortedComments = myPicture.comments.sort{it.content}.reverse()

Has the advantage of allowing unlimited sorting configurations as needed, as opposed to only one as retrieved by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文