Grails one to many情况下子对象排序打赏
Grails是一套用于快速Web应用开发的开源框架,它基于Groovy编程语言,并构建于Spring、Hibernate等开源框架之上,是一个高生产力一站式框架。
近期因团队需要使用Grails做开发,临时学习、熟悉一下Grails,以便快速投入开发,熟悉过程中发现,在Grails的 one-to-many 关系中,自动为你添加java.util.set,因此many端的数据顺序是不一定的,看了官方说是可以用SortedSet,也可以用List,List这里我没测试成功(可能是新接触,配置得不对),SortedSet的方式测试可用,这里记录一下。
假设有文章domain Post,评论domain Comment,Post配置hasMany=[comments:Comment],Comment配置belongTo=[post:Post],如下:
Post
package test class Post { String title String content Date time=new Date() SortedSet comments static hasMany = [comments:Comment] static constraints = { } }
Comment
package test class Comment implements Comparable { String content Date time=new Date() static belongsTo = [post:Post] static constraints = { } @Override int compareTo(Object o) { return time.compareTo(o.time) } }
很简单,就是指定comments为SortedSet,many端domain实现Comparable接口。
- 固定链接:https://www.poorren.com/grails-one-to-many-object-sort
- 文章标签:grails,mapping,order,sort,排序
- 扫二维码:Grails one to many情况下子对象排序
- 版权所有:除"转载分享"分类外,未经注明,均为原创、整理,转载请注明出处。
万分感谢分享!
好~萌菌网www.moejun.tv/