grails域类保存

时间:2012-09-22 17:03:17

标签: grails gorm grails-domain-class

当我尝试在grails中保存域类时出现以下错误:

没有方法签名:java.lang.String.save()适用于参数类型:()值:[] 可能的解决方案:size(),size(),take(int),wait(),any(),wait(long)。 Stacktrace如下:

我有一个将XML字符串分成域对象的服务。然后我尝试保存域并获得该错误。我已经调试并知道所有数据都是有效的。这是我的代码:

   def newProfile="";

      newProfile = new LinkedinProfile(
        firstName               :   "${linkedinProfileFeed.'first-name'}",
        lastName                :   "${linkedinProfileFeed.'last-name'}",
        dobYear                 :   "${linkedinProfileFeed.'date-of-birth'.'year'}",
        dobMonth                :   "${linkedinProfileFeed.'date-of-birth'.'month'}",
        dobDay                  :   "${linkedinProfileFeed.'date-of-birth'.'day'}"  ,   
        imgUrl                  :   "${linkedinProfileFeed.'picture-url'}", 
        siteStandardAddress     :   "${linkedinProfileFeed.'site-standard-profile-request'}",           
        oAuthToken              :   accessTokenKey.toString(),
        secretKey               :   accessTokenSecret.toString()
       )
      .id="${linkedinProfileFeed.id}".toString()

      log.debug("${linkedinProfileFeed.id}".toString())
      log.debug("${linkedinProfileFeed.'first-name'}")
      log.debug("${linkedinProfileFeed.'last-name'}")
      log.debug("${linkedinProfileFeed.'date-of-birth'.'year'}")
      log.debug("${linkedinProfileFeed.'date-of-birth'.'month'}")
      log.debug("${linkedinProfileFeed.'date-of-birth'.'day'}")
      log.debug("${linkedinProfileFeed.'picture-url'}")
      log.debug("${linkedinProfileFeed.'site-standard-profile-request'}")
      log.debug(accessTokenKey.toString())
      log.debug(accessTokenSecret.toString())
      log.debug("end debug")





      newProfile.save();

另外,我不熟悉grails和springsource,但在.Net中,我可以使用点运算符访问对象属性。例如,如果我有一个如上所述的对象,我可以输入newProfile。并且可以访问所有属性。在grails中,这不会发生。是我的代码设计还是错误?

下面的

也是我的域类。

   class LinkedinProfile {
String firstName
String lastName
String dobYear
String dobMonth
String dobDay
String oAuthToken
String secretKey
String id
String imgUrl
String siteStandardAddress
Date dateCreated
Date lastUpdated
long version

static hasMany = [
    linkedinLocation        : LinkedinLocation,
    linkedinSkill           : LinkedinSkill
]

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

    columns {
        firstName           type:'text'
        lastName            type:'text'         
        oAuthToken          type:'text'
        secretKey           type:'text'         
        dobYear             type:'text'
        dobMonth            type:'text'
        dobDay              type:'text'
        imgUrl              type:'text'
        siteStandardAddress type:'text'
    }


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


def beforeInsert = {
//to do:  before insert, capture current email that is stored in the db
}

def afterInsert={
    //resave email
}

}

1 个答案:

答案 0 :(得分:2)

该错误表示您正在对字符串调用save()。如果我们调查,我们会看到你。这是因为你要分配

 newProfile = new LinkedinProfile().id="${linkedinProfileFeed.id}".toString()

所以newProfile实际上是字符串linkedinProfileFeed.id,而不是人们可能期望的new LinkedinProfile()

比较

groovy> def foo = new Post().id="1" 
groovy> foo.class 

Result: class java.lang.String

groovy> def foo = new Post(id: "1") 
groovy> foo.class 

Result: class test.Post

您可能希望将id放入构造函数参数中。无论如何,您需要newProfile作为LinkedinProfile实例结束。然后你可以打电话给save()

同样可以,您可以在Groovy中使用点运算符。