【问题标题】:Grails - MongoDB - IndexOutOfBoundsException occurred when processing requestGrails - MongoDB - 处理请求时发生 IndexOutOfBoundsException
【发布时间】:2012-08-06 16:58:59
【问题描述】:

我正在尝试学习 MongoDB,但在使用嵌入式域时遇到了麻烦。我的应用有一个问题,其中可以包含 0 个选项:

class Question {

    ObjectId id
    String text

    List<Option> optionList = []
    static embedded = ['optionList']
}

class Option {
    ObjectId id

    String text
    static belongsTo = [question: Question]
}

现在,当我想通过以下 2 个选项请求保存问题时:

void testSave() {
    QuestionController questionController = new QuestionController()
    questionController.request.parameters =
        [
                "text": "test question",
                "optionList[0].text": "a",
                "optionList[1].text": "b"
        ]
    questionController.save()
}

这会引发异常:

Invalid property 'optionList[0]' of bean class [groovy.lojzatran.anketa.Question]: Index of out of bounds in property path 'optionList[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
org.springframework.beans.InvalidPropertyException: Invalid property 'optionList[0]' of bean class [groovy.lojzatran.anketa.Question]: Index of out of bounds in property path 'optionList[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

当我使用静态 hasMany 将关系更改为一对多时,效果很好。

谁能帮帮我?

谢谢

【问题讨论】:

    标签: mongodb grails


    【解决方案1】:

    替换这一行:

    List<Option> optionList = []
    

    List<Option> optionList =  new org.springframework.util.AutoPopulatingList<Option>(Option)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 2015-10-24
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      相关资源
      最近更新 更多