2

問題: GORMは、子を1対多の関係で作成(保存)することができます。 このドキュメントの例によると、しかしどこかで私のコードはそれを不可能にするバグです。plsは私がそれを見つけるのを助けます:|

更新: 入力ミスのある文字がいくつか見つかりました。(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)
4

2 に答える 2

1

また、このコードをドメイン クラス (および hasMany プロパティを持つ他のすべてのクラス) に配置する必要があります。

static mapping = {
    chapters(cascade:"all-delete-orphan")
}
于 2012-04-11T10:54:58.687 に答える
1

ガイドの授業でスペルを間違えたようです。おそらく、hadMany ではなくhasManyを意味しています。

于 2012-04-11T10:48:39.653 に答える