gpt4 book ai didi

grails - GORM,一次一对多保存整个对象图不起作用,错误在哪里?

转载 作者:行者123 更新时间:2023-12-02 14:03:32 26 4
gpt4 key购买 nike

问题:
GORM能够与子对象以一对多关系创建(保存)父对象。
Per this documentation example但是我的代码中有一个错误使它不可能。请帮我找到它:|

更新:
找不到几封错字的信件。 (由于STS无法在我的PC上运行...)
但是问题仍然存在。但是我对建议被抛出异常的建议感到困惑。仍然需要帮助。

父模型:

package tanktactics

class Guide {
String title
Date created
SortedSet chapters

static hasMany = [chapters: Chapter] //changed to has

static constraints = {
}
}

子模型:
package tanktactics

class Chapter implements Comparable {
String title
String content
Integer sortOrder
static belongsTo = [guide: Guide]

static constraints = {
}

int compareTo(obj) {
sortOrder.compareTo(obj.compareTo) //changed to sort
}
}

grails console打印输出:
import tanktactics.Guide
import tanktactics.Chapter

some_guide = new Guide(title: "First guide!", created: new Date())
some_guide.addToChapters(new Chapter(title: "Ch II", content: "Lorem II", sortOrder:1))
.addToChapters(new Chapter(title: "Ch I", content: "Lorem", sortOrder:0))
.save()

some_guide.title
some_guide.chapters[0].title
some_guide.chapters[0].content
some_guide.chapters[1].title
some_guide.chapters[1].content

groovy> import tanktactics.Guide
groovy> import tanktactics.Chapter
groovy> some_guide = new Guide(title: "First guide!", created: new Date())
groovy> some_guide.addToChapters(new Chapter(title: "Ch II", content: "Lorem II", sortOrder:1)).addToChapters(new Chapter(title: "Ch I", content: "Lorem", sortOrder:0)).save()
groovy> some_guide.title
groovy> some_guide.chapters[0].title
groovy> some_guide.chapters[0].content
groovy> some_guide.chapters[1].title
groovy> some_guide.chapters[1].content

Exception thrown

groovy.lang.MissingMethodException: No signature of method: tanktactics.Guide.addToChapters() is applicable for argument types: (tanktactics.Chapter) values: [tanktactics.Chapter : null]
Possible solutions: addToChapters(java.lang.Object), getChapters()
at com.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:963)
at com.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1237)
at temp_test.run(temp_test.groovy:6)
at com.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1237)
at com.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1237)

最佳答案

看来您在Guide类中拼写错误。您可能是说hasMany而不是hadMany。

关于grails - GORM,一次一对多保存整个对象图不起作用,错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10104368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com