0
我已經建立了一個用戶界面,允許您在同一個表單上輸入主記錄(VocabularyTest)和一組詳細記錄(VocabQuestion)。我很高興的是,正被提交的數據是正確的,但是當我試圖保存主記錄,我得到以下錯誤Grails:如何堅持主細節關係
null id in vocabularytest.VocabQuestion entry
編輯 - 完整的堆棧跟蹤是
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編輯
我假設這是因爲我的詳細記錄上的外鍵引用爲空,這不會讓我感到意外,因爲我沒有做任何設置。
是被我的控制器接收到的PARAMS如下
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
我的域對象是
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
}
我控制器的相關方法是
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)
}
上午我試圖做一些GORM和Grails將努力做的事情,或者我只是想念有什麼重要的
+1。說明: - 在這種情況下,添加'belongsTo'在父對象中維護一個外鍵約束,即'VocabularyTest'。但它也增加了級聯刪除功能。如果刪除測試,則與測試相關的所有問題也會被刪除。 – dmahapatro
這不適合我。如果我自己添加belongsTo,我會得到完全相同的錯誤。我不能對List進行更改,因爲我在動態地擴展表單上的集合 - 我可以使其工作的唯一方法是使用LazyList。 – DaveH
你必須解釋你在列表中得到的錯誤,因爲我也有動態列表,並且這個工作沒有問題。它所做的只是更改集合的數據類型(從Set到List)。您可以很好地擴展該列表。 '[] .add('b')' –