我想知道当作为webservice请求接收更新请求时,处理现有实体(例如个人)更新请求的最佳方式是什么。
在我的项目中,我有一个Person域类,我想通过CXF插件将CRUD操作公开为webservice操作。所以我创建了一个PersonService,并用
static expose = ['cxfjax']然后我有了update方法:
@WebResult(name = "person")
@WebMethod(operationName = "update")
Person update(@WebParam(name="person")Person person) {
println "Updating $person"
return person.save()
}在服务中,我得到了一个很好的Person对象,但即使它有一个现有person的id,也会创建一个新的person,并更改id以反映这一点。
所以..。如何将收到的人员“合并”到Hibernate会话中,以便Grails将其识别为现有人员?
致以亲切的问候,
克里斯蒂安
发布于 2010-09-10 05:00:53
也许..。
@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()
}
}发布于 2010-09-10 15:02:35
不是100%确定,但是attach()能工作吗?即
@WebResult(name = "person")
@WebMethod(operationName = "update")
Person update(@WebParam(name="person")Person person) {
println "Updating $person"
person.attach();
return person.save()
}我认为这就是该方法的意义所在。
https://stackoverflow.com/questions/3679454
复制相似问题