使用grails添加数据时请帮助null异常

时间:2011-07-21 18:53:05

标签: grails groovy gorm grails-domain-class

我正在尝试将数据添加到数据库中,但是会收到此错误:

错误500:执行controller [mgr.CollectDataEntryController]的action [getData]导致异常:groovy.lang.MissingMethodException:没有方法签名:static groovy.lang.MissingMethodException.addToAlumConnectionEducations()适用于参数类型:(mgr .AlumConnectionEducations)值:[mgr.AlumConnectionEducations:null]

其中CollectDataEntryController是执行工作的控制器。这是该控制器的代码片段:

saveNewAlum.addToAlumConnectionEducations(new AlumConnectionEducations(alumConnectionsId:connectionId, degree:alumConnectionEduDegree,endDate:alumConnectionEduEndDate,fieldOfStdudy:alumConnectionEduFieldOfStudy,schoolName:alumConnectionEduSchoolName,startDate:alumConnectionEduStartDate))

域名AlumConnectionEducations属于与另一个域AlumConnections的一对多关系,AlumConnections又属于与另一个域的一对多关系,alumProfile

to,在我的代码中我首先添加域AlumConnection,它工作正常,但当我尝试添加AlumConnectionEducations时,我得到上述错误。任何人都知道我做错了什么?

感谢 杰森

    package mgr
    import java.util.Date;
    class AlumConnections  {
String linkedinId
String firstName
String lastName
String headline
String locationName
String locationCode
String industry
    Date dateCreated
Date lastUpdated
long version
static belongsTo = [alumProfile:AlumProfile]
static hasMany = [  
alumConnectionEducations    :   AlumConnectionEducations
 ]
static mapping = {
    cache true

    columns {       
        linkedinId      type:'text'
        firstName       type:'text'
        lastName        type:'text'
        headline        type:'text'
        locationName    type:'text'
        locationCode    type:'text'
        industry        type:'text'

    }
}
static constraints = {  
    linkedinId      (nullable:false, blank:false)
    firstName       (nullable:true)
    lastName        (nullable:true)
    headline        (nullable:true)
    locationName    (nullable:true)
    locationCode    (nullable:true)
    industry        (nullable:true)

}
  }         

在调用域的控制器中:

  def saveNewAlum = ""
  saveNewAlum = new AlumProfile(firstName:linkedinFirstName, lastName:linkedinLastName, dob:newDate,industry:linkedinIndustry, oAuthToken:oAuthtoken, secretKey:secretKey)

  saveNewAlum.addToAlumConnections(new   AlumConnections(linkedinId:connectionId,firstName:connectionFName, lastName:connectionLName, headline:connectionHeadline, locationName:connectionLocationname, locationCode:connectionLocationCode,industry:connectionIndustry))

上面的代码工作正常并保存到MySQL数据库。它只有当我尝试创建saveNewAlum.addToAlumConnectionEducations时才会收到错误

1 个答案:

答案 0 :(得分:1)

好吧,它花了一整天但我得到了它。所以,我的假设似乎是正确的,这是导致问题的一对多的一对多。这是我如何解决它。域名是我的方式是正确的。在控制器中: 首先,我创建了主要的父母:

         saveNewAlum=new AlumProfile(firstName:linkedinFirstName, lastName:linkedinLastName, dob:newDate, industry:linkedinIndustry, oAuthToken:oAuthtoken, secretKey:secretKey)

然后,我创建要添加到子项的对象:

     myConnection= new AlumConnections(linkedinId:connectionId,firstName:connectionFName,lastName:connectionLName,headline:connectionHeadline,locationName:connectionLocationname, locationCode:connectionLocationCode,industry:connectionIndustry)

然后,我将孩子保存到父母:

    saveNewAlum.addToAlumConnections(myConnection)

然后,我创建了保存给孩子的孩子的对象:

  newConnectionEdu=  new AlumConnectionEducations(
              degree:alumConnectionEduDegree,
              endDate:alumConnectionEduEndDate,
              fieldOfStudy:alumConnectionEduFieldOfStudy,
              schoolName:alumConnectionEduSchoolName,
              startDate:alumConnectionEduStartDate)

然后,最后,我将孩子添加到孩子身上:

 myConnection.addToAlumConnectionEducations(newConnectionEdu)

这就是你如何为孩子添加孩子!