Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails removeFrom() issue

Inside my Applicant domain class I have the following:

static hasMany = [recommendationFiles:ApplicantFile]
static mapping = {recommendationFiles joinTable: [name:"LETTER_FILES", key: "APPLICANT_ID", column: "LETTER_ID"]}

When I do the following:

    def applicant = Applicant.findByENumber(session.user.eNumber)
    def applicantFiles = applicant.recommendationFiles
    println applicantFiles
    applicantFiles.each {
        applicant.removeFromRecommendationFiles(it)
    }
    applicant.save(flush:true)

I get this as an error which makes no sense to me:

| Error 2015-04-08 10:41:59,570 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver  - ConcurrentModificationException occurred when processi
ng request: [POST] /scholarshipsystem/specialized/index - parameters:
_action_reUpload: Re-Upload
Stacktrace follows:
Message: null
   Line | Method
->> 793 | nextEntry in java.util.HashMap$HashIterator
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   828 | next      in java.util.HashMap$KeyIterator
|   106 | reUpload  in scholarshipSystem.SpecializedController$$EP9HMKbd
|   195 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   895 | runTask   in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . . in     ''
^   662 | run       in java.lang.Thread
like image 391
connor moore Avatar asked Jan 09 '23 14:01

connor moore


1 Answers

There's a few ways to make this work. One way is to convert the list to an array and iterate over it.

def list = applicantFiles.toArray()
list.each {
  applicant.removeFromRecommendationFiles(it)
}

Another way, if you're just removing the entire collection would be to...

applicant.recommendationFiles*.delete()
applicant.recommendationFiles.clear()
like image 115
Gregg Avatar answered Feb 20 '23 13:02

Gregg