Grails OneToMany Set 排序

大家都知道在Hibernate中OneToMany 关系映射是可以选择映射的集合是什么类型的,例如Set 或者List 这里不多做介绍.

最近项目开发使用基于Groovy语言的Grails框架,在domain层实体进行OneToMany关系映射的时候默认,选择的是集合Set,这样导致,我们在自动取得Many对象值是,每次都是不同的顺序,这是我们所不希望的.解决这个方式大致有3种办法:例如 User OneToMany Book

1:在User static mapping 中对 Many 设置排序规则

class User {
    String name
    String password


    static constraints = {
        name nullable: true,null:true
        password nullable: true,null:true
    }


    static hasMany = [books:Book]
    static mapping = {
        books sort: 'id', order: 'desc'
    }
}


class Book {

    String name

    static constraints = {
       name nullable: true,null:true
    }
}
2: 把User 中的Book设置成SortedSet

class User {

    String name
    String password
    SortedSet books

    static constraints = {
        name nullable: true,null:true
        password nullable: true,null:true
    }

    static hasMany = [books:Book]
    static mapping = {
        books sort: 'id', order: 'desc'
    }
}

class Book {
    String name
    static constraints = {
        name nullable: true,null:true
    }
}

3 把book 设置成List

class User {

    String name
    String password
    List books


    static constraints = {
        name nullable: true,null:true
        password nullable: true,null:true
    }

    static hasMany = [books:Book]
    static mapping = {
        books sort: 'id', order: 'desc'
    }
}

class Book {
    String name
    static constraints = {
        name nullable: true,null:true
    }
}

具体问题可以灵活选择.







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值