简体   繁体   中英

Grails domain instance not saving

I have a grails domain object that I validate and then save as such:

if(foo.hasErrors()) {
   transactionStatus.setRollbackOnly()
   respond foo.errors, view: 'create'
   return
}

foo.save flush:true, failOnError:true

println(foo)

There are no errors given on the save. But when I call the println, it says my object is unsaved. However, if I check the database, it has indeed been persisted. Is there something I can check to tell me why grails is telling me it is unsaved. The grails version is 3.0.9.

After more testing, it looks like it is related to the way I specify the key in my domain mapping.

static mapping = {
    version false
    autoTimestamp false
    id name:'foo_id', generator:'increment'
}

If I remove the id field and let Grails handle it by default the object is saved properly. If I add the id field back, it has the issue described above. Is there a reason for it?

I wouldn't be too concerned about the format of toString() . You're calling save with failOnError: true , so it's safe to assume that an exception would be thrown if persistence fails. If you don't specify this argument, you can check that persistence succeeds like so

if (foo.save()) {
  println 'it worked'
} else {
  println 'if failed'
}

try

foo = foo.save flush:true, failOnError:true

println(foo)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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