Grails:从后台线程访问 GORM

发布于 2024-11-29 20:23:34 字数 1012 浏览 2 评论 0原文

我已经为这个错误苦苦挣扎了一个星期了,我真的为此失去了理智!我已经尝试了多种实现、解决方法和黑客等等,但我只是不断地尝试另一个异常。

我正在使用 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 实现。没有 hasManybelongsTo - 因为由于性能问题,我在之前的帖子中对此感到沮丧。

我想我已经尝试了 savemergewithTransachtionPersistenceContextInterceptor 的所有可以想到的组合...

这是怎么回事应该工作吗?请举例。

提前致谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-12-06 20:23:34

在新线程中工作似乎不是问题,它看起来像是一个标准验证问题。它表示 Page 为 null,这表示验证错误,因为 save() 如果成功则返回实例,如果存在一个或多个验证错误,则返回 null。有几个选项:

def page = new Page(type : Page.TYPE_TABLE,
     domain: dbUpdate.domainVersion.domain, identifier: tableName)
page.save(flush:true)
if (page.hasErrors()) {
   // handle errors
}
else {
   def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
       con, tableName, dbUpdate.author).save(flush:true)
}

或者使用 failOnError 抛出异常:

def page = new Page(type : Page.TYPE_TABLE, identifier: tableName,
     domain: dbUpdate.domainVersion.domain).save(flush:true, failOnError: true)
def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
    con, tableName, dbUpdate.author).save(flush:true)

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, or null if there's a one or more validation errors. There are a few options:

def page = new Page(type : Page.TYPE_TABLE,
     domain: dbUpdate.domainVersion.domain, identifier: tableName)
page.save(flush:true)
if (page.hasErrors()) {
   // handle errors
}
else {
   def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
       con, tableName, dbUpdate.author).save(flush:true)
}

or use failOnError to throw an exception:

def page = new Page(type : Page.TYPE_TABLE, identifier: tableName,
     domain: dbUpdate.domainVersion.domain).save(flush:true, failOnError: true)
def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
    con, tableName, dbUpdate.author).save(flush:true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文