Quantcast

How do you get the id after saving a domain object?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How do you get the id after saving a domain object?

Daniel Glauser
Hello,

I was under the impression that this would probably work:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save()
assert comp.id

I was under the impression that this would definitely work if you call flush:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save(flush:true)
assert comp.id

Either way we run it the code blows up on the assert.  We tried this from within a non-transactional service and a controller.
Is there something I'm missing here?

Thanks,
Daniel

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How do you get the id after saving a domain object?

burtbeckwith
That should work unless there's a validation error. Are you sure the object saved? Check comp.hasErrors() and you can also use save(failOnError: true).

Burt

> Hello,
>
> I was under the impression that this would probably work:
>
> Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the
> other thing")
> comp.save()
> assert comp.id
>
> I was under the impression that this would definitely work if you call
> flush:
>
> Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the
> other thing")
> comp.save(flush:true)
> assert comp.id
>
> Either way we run it the code blows up on the assert.  We tried this from
> within a non-transactional service and a controller.
> Is there something I'm missing here?
>
> Thanks,
> Daniel
>

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How do you get the id after saving a domain object?

basejump (Josh)
In reply to this post by Daniel Glauser
yup
either make sure save is returning true (you didn't get validation errors)
or set grails.gorm.failOnError=true in Config.groovy


On Aug 25, 2010, at 4:30 PM, Daniel Glauser wrote:

Hello,

I was under the impression that this would probably work:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save()
assert comp.id

I was under the impression that this would definitely work if you call flush:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save(flush:true)
assert comp.id

Either way we run it the code blows up on the assert.  We tried this from within a non-transactional service and a controller.
Is there something I'm missing here?

Thanks,
Daniel


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How do you get the id after saving a domain object?

Daniel Glauser
Hi Josh,

Yeah, you pretty much nailed it on the head.
We were calling validate ahead of time thinking that would catch any errors.  We were mistaken.

Bad code:

void validateAndSave(boolean flushStatus) {
    if (this.validate()) {
      this.save(flush:flushStatus)
    }
    else {
      println("Error saving " + this)
      println this.errors
    }
  }

Good code:

void validateAndSave(boolean flushStatus) {
    def savedObj
    def valid = this.validate()

    if (valid) {
      savedObj = this.save(flush:flushStatus)
    }

    if(!valid || !savedObj) {
      println("Error saving " + this)
      println this.errors
    }

    savedObj
  }

Thanks for your help,
Daniel



On Wed, Aug 25, 2010 at 4:03 PM, basejump <[hidden email]> wrote:
yup
either make sure save is returning true (you didn't get validation errors)
or set grails.gorm.failOnError=true in Config.groovy


On Aug 25, 2010, at 4:30 PM, Daniel Glauser wrote:

Hello,

I was under the impression that this would probably work:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save()
assert comp.id

I was under the impression that this would definitely work if you call flush:

Component comp = new Component(diss:"diss", that:"that", theOtherThing:"the other thing")
comp.save(flush:true)
assert comp.id

Either way we run it the code blows up on the assert.  We tried this from within a non-transactional service and a controller.
Is there something I'm missing here?

Thanks,
Daniel



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How do you get the id after saving a domain object?

Daniel Glauser
And in the ever so popular closure format (and minus the return type bug):

def validateAndSave = { flushStatus ->
    def savedObj
    def valid = this.validate()

    if (valid) {
      savedObj = this.save(flush:flushStatus)
    }

    if(!valid || !savedObj) {
      println("Error saving " + this)
      println this.errors
    }

    savedObj
  }

Cheers,
Daniel

Loading...