0
votes

I am having some difficulties adding children domains to parents. here are the classes:

  class parent{
String firstName
String lastName
String dobYear
String dobMonth
String dobDay
Date dateCreated
Date lastUpdated
long version

static hasMany = [
    chidlren: Children

]

static mapping = {
    cache true
    id generator: 'assigned'

    columns {
        firstName           type:'text'
        lastName            type:'text'
        dobYear             type:'text'
        dobMonth            type:'text'
        dobDay              type:'text'
    }


}
static constraints = {
    firstName           (nullable:true)
    lastName            (nullable:true)     
    dobYear             (nullable:true)
    dobMonth            (nullable:true)
    dobDay              (nullable:true)     
    id              (nullable:false)

}

}

children:

   class Children{

String skillId
String skillName    
String skillProficiency
String skillYears       
Date dateCreated
Date lastUpdated
long version

static belongsTo = [parent:Parent]
static mapping = {
    cache true
    columns {       
        skillId      type:'text'
        skillName    type:'text'
        skillProficiency type:'text'            
        skillYears   type:'text'
    }
}
static constraints = {

     skillId                    (nullable:true)
     skillName                  (nullable:true)
     skillProficiency           (nullable:true) 
     skillYears                 (nullable:true) 
}

}

I then instantiate the parent class like this: //my xml def feed= new XmlSlurper().parseText(linkedinResponse);

     def newParent= new Parent(
        firstName:"${feed.'first-name'}",
        lastName:"${feed.'last-name'}",
        dobYear :"${feed.'date-of-birth'.'year'}",
        dobMonth:"${feed.'date-of-birth'.'month'}",
        dobDay  :"${feed.'date-of-birth'.'day'}"        

       )
      .id="${feed.id}".toString()

In my xml feed, i have multiple children nodes so i want to create multiple domain nodes and add them to the parent. The xml stuff works fine:

  feed.skills.skill.each{ mySkill ->
def newChild = new Children(
              skillId:  mySkill.'id',
              skillName:    mySkill.'name',
              skillProficiency: mySkill.'proficiency',
              skillYears    :   mySkill.'years'
              )
          newParent.addToChildren(newChild )
      }

When i try to add the child, i get an error (o signature of method: com.myapp.Children.call() is applicable for argument types: () values: [])

Other then extra field, how is this any different from the examples i find on the grails site, like this:

 def parent = new Parent(name:'Dad')
 parent.addToChildren(new Child(name:'son'))
 parent.addToChildren([name:'daughter'])

Thanks for any help

jason

1
do you save the parent after adding the children?lucke84
no, i dont save the parent first. right now, i want to just create the domain classes, fill them, and save them to a database at another point in the applicationjason

1 Answers

0
votes

Maybe a typo. In your sample you can read:

static hasMany = [
    chidlren: Children
]

Should be:

static hasMany = [
    children: Children
]