加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Grails one-to-many 排序

发布时间:2020-12-14 16:45:15 所属栏目:大数据 来源:网络整理
导读:在grails domain中,如下方法可用于one-to-many时对many一方数据进行排序: 在one的一方的domain中设置SortedSet属性,值为many一方的集合,使用static hasMany指明many一方的domain类。 在many一方的domain中,实现Comparable接口,实现compareTo方法。 在o

在grails domain中,如下方法可用于one-to-many时对many一方数据进行排序:

  • 在one的一方的domain中设置SortedSet属性,值为many一方的集合,使用static
    hasMany指明many一方的domain类。
  • 在many一方的domain中,实现Comparable接口,实现compareTo方法。
  • 在one的一方同时可以在mapping中设置many一方的lazy为true/false。

代码如下:
one的一方的domain:

class Twitter {
    String content
    Date dateCreated
    Date lastUpdated

    SortedSet comments

    static constraints = {
        content(nullable: false,blank: false)
    }
    static hasMany = [comments:TwitterComment]
    static mapping = {
        version(false)
        comments(lazy:false)
    }
}

many的一方的domain(此处为双向关联):

class TwitterComment implements Comparable<TwitterComment>{

    Twitter twitter
    String content

    Date dateCreated
    Date lastUpdated

    static constraints = {
        twitter(nullable: false)
        content(nullable: false)
    }
    static belongsTo = [
            twitter:Twitter
    ]

    @Override
    int compareTo(TwitterComment o) {
        return o.dateCreated.compareTo(dateCreated)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读