Grails:从后台线程访问 GORM
我已经为这个错误苦苦挣扎了一个星期了,我真的为此失去了理智!我已经尝试了多种实现、解决方法和黑客等等,但我只是不断地尝试另一个异常。
我正在使用 Executor 插件异步运行方法:
runAsync{
run(...)
}
该方法最初删除一些对象:
page.delete(flush:true)
然后可能重新创建这些对象:
def page = new Page(type : Page.TYPE_TABLE, domain : domainVersion.domain, identifier : tableName)
page.save(flush: true, failOnError: true)
但是失败并出现以下异常:
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.ramboll.egs.ohs.domain.Domain#1]
Page
和 之间的关系>Domain
简单地由具有 Domain
属性的 Page
实现。没有 hasMany
和 belongsTo
- 因为由于性能问题,我在之前的帖子中对此感到沮丧。
我想我已经尝试了 save
、merge
、withTransachtion
和 PersistenceContextInterceptor
的所有可以想到的组合...
这是怎么回事应该工作吗?请举例。
提前致谢!
I have been struggling with this error for a week now, and I am seriously losing my mind over this! I have tried multible implementations and work-arounds and hacks and what not, but I just keep stubling into just another exception.
I am using the Executor plugin to run a method asynchroniously:
runAsync{
run(...)
}
The method initially deletes some objects:
page.delete(flush:true)
And then later possibly recreating those objects:
def page = new Page(type : Page.TYPE_TABLE, domain : domainVersion.domain, identifier : tableName)
page.save(flush: true, failOnError: true)
But that fails with the following exception:
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.ramboll.egs.ohs.domain.Domain#1]
The relationship between the Page
and Domain
is simply implemented by Page
having a Domain
attribute. No hasMany
og belongsTo
- as I was discouraged from this in an earlier post due to performance issues.
I think I have tried all thinkable combinations of save
, merge
, withTransachtion
and PersistenceContextInterceptor
...
How is this supposed to work? Examples please.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在新线程中工作似乎不是问题,它看起来像是一个标准验证问题。它表示
Page
为 null,这表示验证错误,因为 save() 如果成功则返回实例,如果存在一个或多个验证错误,则返回null
。有几个选项:或者使用
failOnError
抛出异常:It doesn't appear that working in a new thread is the issue, it looks like a standard validation problem. It's saying that the
Page
is null, which indicates a validation error since save() returns the instance if it was successful, ornull
if there's a one or more validation errors. There are a few options:or use
failOnError
to throw an exception: