2012-08-06 57 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