przemo_li
przemo_li

Reputation: 4053

GORM, one-to-many saving whole object graph at once do not work, where is bug?

Problem: GORM is able to create (save) parent object with its child in one-to-many relation. Per this documentation example But somewhere I my code is bug that make it impossible. Pls help me find it :|

UPDATE: Found few miss-typed letters. (Since STS do not work on my PC...) But problem remains the same. However I'm puzzled at suggestion which is given exception is thrown. Still need help.

Parent model:

package tanktactics

class Guide {
    String title
    Date created
    SortedSet chapters

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

    static constraints = {
    }
}

Child model:

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
    }
}

Printout from 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)

Upvotes: 2

Views: 512

Answers (2)

matcauthon
matcauthon

Reputation: 2261

You should also put this code into your domain class (and in all other classes with hasMany properties):

static mapping = {
    chapters(cascade:"all-delete-orphan")
}

Upvotes: 1

olexa.le
olexa.le

Reputation: 1807

It seems that you misspelled in Guide class. Possibly you mean hasMany instead of hadMany.

Upvotes: 1

Related Questions