0
假设我有以下域课后:Grails的(StaleObjectStateException) - 设置域对象的属性依赖于它的ID保存
@Entity
class TestDomain {
public static final ID_PREFIX = "prefix-"
String uniqueId
constriaints = {
uniqueId nullable: true
}
}
通过Grails的默认域类确实也有id
财产。在此之后,我想以这种方式设置uniqueId
,即当我创建新的TestDomain
对象时,uniqueId
属性包含第一个创建的对象,如prefix-1
,第二个为prefix-2
,依此类推。
我的方法是认识到内save
行动TestDomainController
的:
def save(TestDomain testDomainInstance) {
if (testDomainInstance == null) {
notFound()
return
}
if (testDomainInstance.hasErrors()) {
respond testDomainInstance.errors, view:'create'
return
}
testDomainService.save(testDomainInstance) //.save flush:true
testDomainInstance.uniqueId = TestDomain.ID_PREFIX + testDomainInstance.id
testDomainService.save(testDomainInstance) //.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'TestDomain.label', default: 'Test Domain'), testDomainInstance.id])
redirect testDomainInstance
}
'*' { respond testDomainInstance, [status: CREATED] }
}
}
但是,一旦我救我收到以下错误的对象:
StaleObjectStateException occurred when processing request: ...
Message
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [test.TestDomain#1]
现在,我能怎么办呢让这个运行?或者,是否有可能以更美观的方式设置uniqueId
的值?目前,我有点卡住了。
谢谢你的帮助!
编辑:
我现在为了设置uniqueId
有试图redirect
到另一个动作第一save
后。但是,我得到了同样的错误。即使我在testDomainService
内使用save(flush: true)
,事情根本不会改变。
另一种方法是将所有的第一次保存,更改和第二次保存到的方法,但没有成功 - 发生同样的错误。该服务是交易性的。
通常不能保存域对象,改变它并将其保存在同一个事务中?
我试过了,但我得到了同样的错误(StaleObjectStateException)......它在你的应用程序中工作吗? – gabriel 2014-11-04 13:12:27
org.hibernate.StaleObjectStateException如果同一个对象被两个线程同时修改,请阅读本节。 – 2014-11-24 11:08:05