[英]Sort by property in one-to-many association
I have the following scenario (Domain classes) 我有以下情况(域类)
class Request {
String title
List statusUpdates
static hasMany = [statusUpdates: StatusUpdate]
}
and 和
class StatusUpdate {
Date dateCreated
String statusFrom
String statusTo
String notes
static belongsTo = Request
}
I am currently using createCriteria
to implement filtering and basic sorting of Request. 我目前正在使用
createCriteria
实施请求的过滤和基本排序。
Now I want to get a list of Request
s starting from the most recently updated (ordered by the value of the field dateCreated
of the last
StatusUpdate
for the Request
) 现在,我想获取一个从最近更新开始的
Request
的列表(按该Request
的last
StatusUpdate
dateCreated
字段的值排序)
Any hint? 有什么提示吗?
assuming that every Request has at least one StatusUpdate and slightly changing belongsTo declaration, you could go the other direction: 假设每个请求至少有一个StatusUpdate并稍有变化的EmiratesTo声明,则可以选择另一个方向:
class StatusUpdate {
Date dateCreated
String statusFrom
String statusTo
String notes
static belongsTo = [req: Request] // I assume that naming the field "request" would cause whole bunch of weird problems, therefore "req"
}
StatusUpdate.createCriteria().list() {
req {
// your criteria for requests filtering
}
projections {
groupProperty 'req', 'reqAlias'
max 'dateCreated', 'maxDateCreatedAlias'
}
order 'maxDateCreatedAlias', 'desc'
}.collect { it[0] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.