java 休眠/GORM:flush() 未处理集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1701600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:40:11  来源:igfitidea点击:

Hibernate/GORM: collection was not processed by flush()

javahibernategrailsgorm

提问by Dónal

I have an integration test in my Grails application that fails when I try to save an entity of type Member

我的 Grails 应用程序中有一个集成测试,当我尝试保存类型的实体时失败 Member

invitingMember.save(flush: true)

This raises the following exception

这引发了以下异常

org.hibernate.AssertionFailure: collection [com.mycompany.facet.Facet.channels] was not processed by flush() at com.mycompany.member.MemberConnectionService.addOrUpdateContact(MemberConnectionService.groovy:939)

org.hibernate.AssertionFailure:com.mycompany.member.MemberConnectionService.addOrUpdateContact(MemberConnectionService.groovy:939) 处的flush() 未处理集合[com.mycompany.facet.Facet.channels]

Earlier in the transaction I add an object to a collection property of invitingMember. My guess is that the exception is thrown at the line above, because it's only at this point that the object added to the collection is persisted.

在交易的早些时候,我将一个对象添加到invitingMember. 我的猜测是在上面的行抛出异常,因为只有在这一点上,添加到集合中的对象才会被持久化。

回答by Aaron Digulla

The underlying problem is probably that Hibernate doesn't cascade the save. So when you flush the Member, Hibernate notices that the collection is still dirty (which is probably not what you want). So either flush the collection manually or tell Hibernate to cascade all the updates.

潜在的问题可能是 Hibernate 没有级联保存。因此,当您刷新 时Member,Hibernate 会注意到该集合仍然是脏的(这可能不是您想要的)。所以要么手动刷新集合,要么告诉 Hibernate 级联所有更新

回答by unludo

I found a possible solution here.

我在这里找到了一个可能的解决方案。

Actually, look at toString, equals, hashcode and make sure you are not accessing your lazy accessor.

实际上,查看 toString、equals、hashcode 并确保您没有访问您的惰性访问器。

I know I am talking about java and here the question is about grails but maybe there is the same kind of thing for grails.

我知道我在谈论 Java,这里的问题是关于 grails,但也许 grails 也有同样的事情。

Don't hesitate to edit.

不要犹豫编辑。

It was the issue for me (in java)

这对我来说是个问题(在 Java 中)

回答by IgniteCoders

I had that error when a tried to add element to a collection after delete elements from the collection. I solved it by calling save(flush:true)before add new elements to the collection.

当从集合中删除元素后尝试将元素添加到集合时,我遇到了该错误。我通过save(flush:true)在向集合添加新元素之前调用来解决它。

  • In my case, the collection is always returned from a select.
  • 就我而言,集合总是从select.

回答by leebutts

The only time I've encountered that exeception is when using Hibernate events e.g. beforeInsert, beforeUpdate etc - are you using these?

我唯一遇到过这种情况的时候是在使用 Hibernate 事件时,例如 beforeInsert、beforeUpdate 等——你在使用这些吗?

There's an issue in JIRA related to this which has been fixed for Grails 1.2

JIRA 中存在与此相关的问题,该问题已针对 Grails 1.2 修复