gpt4 book ai didi

grails - Grails GORM/HQL-从关联中检索数据

转载 作者:行者123 更新时间:2023-12-02 15:38:40 25 4
gpt4 key购买 nike

我有一个Blog域类,其中包含许多消息:

class Blog {

String description

static hasMany = [messages : Message]
static belongsTo = [owner : User]

static constraints = {
description blank: true, nullable: true
}
}

class Message {

String content
String title
User author

Date dateCreated
Date lastUpdated

static hasMany = [comments : Comment]

static constraints = {
content blank: false
author nullable: false
title nullable: false, blank: false
}

static mapping = {
content type: "text"
sort dateCreated: 'desc'
}
}

消息还在应用程序的其他地方使用,因此关联是单向的。如何获得20条最新博客消息(按创建日期排序)?所谓最新博客消息,是指与任何博客相关的20条最新消息。

最佳答案

class Blog {
...
...
static hasMany [messages: BlogMessages]
...
...
}

class Message {
...
// exactly like you have it
...
}

class BlogMessage extends Message {
Blog blog
}

然后,您可以像这样获取...
BlogMessage.list([max:20])

关于grails - Grails GORM/HQL-从关联中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5648735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com