简体   繁体   English

在一对多关联中按属性排序

[英]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的列表(按该Requestlast 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] }

You can use Comparator or Comparable interface. 您可以使用比较器或可比较界面。 Here you have a nice example link 在这里,您有一个不错的示例链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM