Grails: Data is updated when try to sort a list -
i have been facing situation following code block has been behaving in strange manner. in following code snippet, when trying sort activity list of case work flow in taglib, perform db update instead of sorting data out. updates version of workflow row. can please suggest me missing anything? quick highly appreciated.
taglib:
class caseformtaglib { static namespace = 'caseform' def caseform = { attr, body -> def caseworkflow = caseworkflow.read(attr.workflowid) //line causing issue def activitylist = caseworkflow?.sortedactivitylist } }
domain:
class caseworkflow { list caseactivitylist static hasmany = [caseactivitylist: caseactivity] @transient def getsortedactivitylist(){ collections.sort(this.caseactivitylist) return this.caseactivitylist } } class caseactivity implements comparable { /** * activity id */ integer activityid @override public int compareto(obj) { if(!obj || !obj.activityid) { return 1 } else if (!this.activityid) { return -1 } else { return this.activityid.compareto(obj.activityid) } } }
this behavior because of read()
being used in taglib. read()
not flush
domain database automatically unless there change associations.
in case, since sorting associations, dirty check done on associations , object automatically flushed. read through description link above, mainly:
there 1 exception though - associated collections, example author's books collection, participate in automatic flushing , dirty detection. because mapped collections treated differently regular properties , manage own dirty checking independent of containing domain class.
moreover, default associations set
in domain class. if want associations sorted can make them sortedset
making comparable
work same way have defined caseactivity
:
class caseworkflow { sortedset caseactivitylist static hasmany = [caseactivitylist: caseactivity] }
is mandatory have indexed using list
?
Comments
Post a Comment