0
votes

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?

2
Did you debug it or just check by println? Moreover which version of Grails? Is function @Transactional? - Michal_Szulc
Does the status change after you commit the transaction? - Gregor Petrin
This is grails 3.0.9 . The function was @Transactional, but I removed all the transactional pieces and have the same issue. - Lithium

2 Answers

0
votes

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'
}
0
votes

try

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

println(foo)