grails domain class save
-
03-07-2021 - |
문제
I get the following error when I try to save a domain class, in grails:
No signature of method: java.lang.String.save() is applicable for argument types: () values: [] Possible solutions: size(), size(), take(int), wait(), any(), wait(long). Stacktrace follows:
I have a service that pareses an XML string into domain objects. I then try to save the domain and get that error. I've debugged and know that all the data is valid. Here is my code:
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();
Also, I'm not to familiar with grails and springsource, but in .Net, I can access an objects properties using the dot operator. For example, if I had an object as described above, I could just type newProfile. and would have access to all the properties. In grails this doesnt happen. is this by design or an error in my code?
below is my domain class, as well.
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
}
}
해결책
The error indicates you are calling save()
on a String. If we investigate, we see that you are. This is because you are assigning
newProfile = new LinkedinProfile().id="${linkedinProfileFeed.id}".toString()
and so newProfile is actually the String linkedinProfileFeed.id
, not a new LinkedinProfile()
as one might expect.
Compare
groovy> def foo = new Post().id="1"
groovy> foo.class
Result: class java.lang.String
with
groovy> def foo = new Post(id: "1")
groovy> foo.class
Result: class test.Post
You probably want to put the id
into the constructor arguments. Regardless, you need newProfile
to end up as a LinkedinProfile
instance. Then you can call save()
on it.
Also yes, you can use the dot operator in Groovy.