【问题标题】:Grails removeFrom() issueGrails removeFrom() 问题
【发布时间】:2015-04-09 17:19:27
【问题描述】:

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

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

【问题讨论】:

  • 你正在从你正在迭代的同一个列表中删除
  • 如何在不迭代它们并对它们中的每一个都执行 removeFrom 的情况下删除它们?

标签: grails grails-orm


【解决方案1】:

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

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

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

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

【讨论】:

  • 第二种方法正是我需要做的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
相关资源
最近更新 更多