2012-08-06 106 views
0

我正在尝试学习MongoDB,但在使用嵌入式域时遇到了麻烦。我的应用程序有一个问题,它可以包含0许多选项:Grails - 处理请求时发生MongoDB - IndexOutOfBoundsException

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将关系更改为一对多时,它运行良好。

任何人都可以帮到我吗?

感谢

回答

2

替换此行:

List<Option> optionList = [] 

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

这可能不是正确的解决方案。 :| – 2012-08-06 19:53:56

+0

看起来像你的解决方案:)非常感谢你! – 2012-08-06 20:59:17