繁体   English   中英

grails域类保存

[英]grails domain class save

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

没有方法签名:java.lang.String.save()适用于参数类型:()values:[]可能的解决方案:size(),size(),take(int),wait(),any(),等待(长)。 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
}

}

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

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

所以newProfile实际上是String 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中使用点运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM