控制器逻辑:如何解决StaleObjectStateException使用JPA和Hibernate
def updateObject() {
Object o = Object.get(params.id as Long)
o.otherObjects.clear()
objectDataService.saveObject(o.id)
OtherObject newObject = new OtherObject;
o.addToOtherObjects(newObject)
objectDataService.saveObject(o.id)
}
ServiceLogic
def saveObject(long profileId) {
o.save(flush:true)
}
什么的情况下,这只是工作的90%发生
。
问题
ERROR errors.GrailsExceptionResolver - StaleObjectStateException occurred when processing request: [GET] /controller/updateObject - parameters:
stuff[]: data
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1].
Stacktrace follows:
Message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1]
我已经通过相关的问题阅读,发现merge
叫你在上面看到。它解决了大约50%的案件,但不是全部。
如果你将这个逻辑转移到一个服务方法中,那么你会不会更好? – 2014-09-03 15:29:46
我不想超载该服务。你认为这有意义吗? – 2014-09-03 15:36:44
将业务逻辑转移到服务并使您的控制器尽可能瘦(通过瘦我的意思是用更少的代码行)总是更好。这将有助于您在任何需要的地方重复使用代码,并在测试中节省大量时间。 – 2014-09-03 19:41:57