views:

33

answers:

2

Hi,

I was wondering what is the best way to handle a request for an update on an existing entity (e.g. a Person) when receiving the request as a webservice request.

In my project I have a Person domain class and I would like to expose CRUD operations as webservice operation through the CXF plugin. So I make a PersonService, and expose the methods with

static expose = ['cxfjax']

Then I have the update method:

@WebResult(name = "person")
@WebMethod(operationName = "update")
Person update(@WebParam(name="person")Person person) {
  println "Updating $person"
  return person.save()
}

In the service I get a fine Person object, but even if it has an id of an existing person, a new person is created, and the id is changed to reflect this.

SO... how do I get the person I receive "merged" into the Hibernate session, so that Grails will recognise it as an existing Person?

Kind regards,

Christian

A: 

maybe...

@WebResult(name = "person")
@WebMethod(operationName = "update")
Person update(@WebParam(name="person")Person person) {

  println "Updating $person"
  def p = Person.get(person.id)
  if ( p ) {
     // what ever your merge logic is...
  } else {
     return person.save()
  }
}
Aaron Saunders
I was hoping that somehow you could kind of re-attach the incoming Person to a Hibernate session, based on the ID. Right now, I'm actually doing like you suggest: Person p = new Person(); p.properties = incomingPerson.properties; p.save();
so did it work?
Aaron Saunders
A: 

Not 100% sure but does attach() work? ie

@WebResult(name = "person")
@WebMethod(operationName = "update")
Person update(@WebParam(name="person")Person person) {
  println "Updating $person"
  person.attach();
  return person.save()
}

I think that is the point of the method.

Scott Warren