简体   繁体   English

Grails:如何保持主从关系

[英]Grails : How to persist Master Detail Relationship

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. 我建立了一个UI,可让您在同一表单上输入主记录(VocabularyTest)和一组详细记录(VocabQuestion)。 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? 我是在尝试执行GORM和Grails始终难以做到的事情,还是只是缺少一些重要的东西?

Try this: 尝试这个:

class VocabularyTest {

    static hasMany = [testItems: VocabQuestion] 

    static constraints = {
    }

    String title 
    List testItems
}

class VocabQuestion {

    static constraints = {

    }
    static belongsTo = [vocabularyTest: VocabularyTest]  

    String question
    String answer
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM