Grails GORM/HQL-从关联中检索数据
我有一个Blog域类,其中包含许多消息:Grails GORM/HQL-从关联中检索数据,grails,hql,gorm,Grails,Hql,Gorm,我有一个Blog域类,其中包含许多消息: class Blog { String description static hasMany = [messages : Message] static belongsTo = [owner : User] static constraints = { description blank: true, nullable: true } } class Message { Strin
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条与任何博客相关的最新消息
def latestMessages = Message.listOrderByDateCreated(max:20, order:"desc")
然后你就可以这样拿
BlogMessage.list([max:20])
这很好,但是还有其他类也与Message关联,我只想要那些与blog关联的消息。我在想,除了继承,可能还有其他方法来实现我的目标。
BlogMessage.list([max:20])