0

I have built a UI that allows you to enter a Master Record ( VocabularyTest ) and a set of Detail Records ( VocabQuestion ) on the same form. I'm pretty happy that the data that is being submitted is correct, but when I try to save the Master record, I get the following error

null id in vocabularytest.VocabQuestion entry 

EDIT - The Complete stacktrace is

null id in vocabularytest.VocabQuestion entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in vocabularytest.VocabQuestion entry (don't flush the Session after an exception occurs)
    Line | Method
->>   24 | save      in vocabularytest.VocabularyController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    195 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

END EDIT

I'm assuming that this is happening as the Foreign Key reference on my Detail record is null, which wouldn't surprise me unduly, as I'm doing nothing to set it.

The Params that are being received by my controller are as follows

title: Dave New Test
testItems[0].question: Q1
testItems[0].answer: A1
testItems[1].question: Q2
testItems[1].answer: A2
testItems[2].question: Q3
testItems[2].answer: A3
create: Create

My domain Objects are

class VocabularyTest {

    static hasMany = [testItems: VocabQuestion] 
    static constraints = {
    }

    String title;
    List <VocabQuestion>testItems = LazyList.decorate(
                                      new ArrayList(),
                                      FactoryUtils.instantiateFactory(VocabQuestion.class));

}
class VocabQuestion {

    static constraints = {

    }
    String question
    String answer
    VocabularyTest vocabularyTest

}

The relevant method of my controller is

def save() {
        log.debug(params)
        def vocabularyTestInstance = new VocabularyTest(params)
        if (!vocabularyTestInstance.save(flush: true)) {
            render(view: "create", model: [vocabularyTestInstance: vocabularyTestInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'vocabularyTest.label', default: 'VocabularyTest'), vocabularyTestInstance.id])
        redirect(action: "index", id: vocabularyTestInstance.id)
    }

Am I trying to do something that GORM and Grails will allways struggle to do, or am I just missing something vital?

4

1 回答 1

0

尝试这个:

class VocabularyTest {

    static hasMany = [testItems: VocabQuestion] 

    static constraints = {
    }

    String title 
    List testItems
}

class VocabQuestion {

    static constraints = {

    }
    static belongsTo = [vocabularyTest: VocabularyTest]  

    String question
    String answer
}
于 2013-05-29T20:26:45.890 回答