Grails removeFrom()问题

时间:2015-04-09 17:19:27

标签: grails gorm

在我的申请人域类中,我有以下内容:

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

当我执行以下操作时:

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

我认为这是一个对我没有意义的错误:

| 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

1 个答案:

答案 0 :(得分:6)

有几种方法可以完成这项工作。一种方法是将列表转换为数组并迭代它。

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

另一种方式,如果您只是删除整个集合将是...

applicant.recommendationFiles*.delete()
applicant.recommendationFiles.clear()