Hibernate java.lang.ClassCastException:org.hibernate.action.EntityIdentityInsertAction 无法转换为 org.hibernate.action.EntityInsertAction

发布于 2024-12-05 01:09:39 字数 1021 浏览 9 评论 0原文

我将 Hibernate 与 EntityManager 一起使用。当我使用时

    Session session = (Session)entityManager.getDelegate();  
    session.flush();
    session.clear();

,我得到

java.lang.ClassCastException: org.hibernate.action.EntityIdentityInsertAction cannot be cast to org.hibernate.action.EntityInsertAction
at org.hibernate.engine.ActionQueue$InsertActionSorter.sort(ActionQueue.java:636)
at org.hibernate.engine.ActionQueue.sortInsertActions(ActionQueue.java:369)
at org.hibernate.engine.ActionQueue.sortActions(ActionQueue.java:355)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:224)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)

由于它没有说明哪个实体导致了问题,所以我被困在这里。有谁知道是什么原因造成的?

I'm using Hibernate with an EntityManager. When I use

    Session session = (Session)entityManager.getDelegate();  
    session.flush();
    session.clear();

I get

java.lang.ClassCastException: org.hibernate.action.EntityIdentityInsertAction cannot be cast to org.hibernate.action.EntityInsertAction
at org.hibernate.engine.ActionQueue$InsertActionSorter.sort(ActionQueue.java:636)
at org.hibernate.engine.ActionQueue.sortInsertActions(ActionQueue.java:369)
at org.hibernate.engine.ActionQueue.sortActions(ActionQueue.java:355)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:224)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)

Since it doesn't say which entity causes the problem, I'm stuck here. Does anyone have an idea what can cause this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

篱下浅笙歌 2024-12-12 01:09:39

这是 Hibernate 中的一个错误。当满足以下条件时抛出异常:

  • id 生成策略是 identity
  • 实体保存在事务之外
  • hibernate.order_insertstrue

发生这种情况是因为 < code>EntityIdentityInsertAction 可以添加到 ActionQueue.insertions 列表中,而 ActionQueue$InsertActionSorter 期望它只包含EntityInsertAction

似乎尚未报告此错误,因此请随时报告

也许您可以更改 hibernate.order_inserts 的值作为解决方法。

It's a bug in Hibernate. Exception is thrown when following conditions are met:

  • id generation strategy is identity
  • entity is saved outside of transaction
  • hibernate.order_inserts is true

It happens because EntityIdentityInsertAction can be added to the ActionQueue.insertions list, whereas ActionQueue$InsertActionSorter expects that it contains only EntityInsertActions.

It looks like this bug was not reported yet, so feel free to report it.

Perhaps you can change the value of hibernate.order_inserts as a workaround.

独﹏钓一江月 2024-12-12 01:09:39

我收到此错误只是因为我忘记在调用 em.persist(myNewEntity) 的方法上添加 @Transactional 注释,因为我认为所有调用者都将在 Transaction 上

,错误不会直接弹出,但是当我尝试从表中删除所有行时,

该错误确实具有误导性,但与 @axtavt 答案

所以

确保保存操作在活动事务中完成

I got this error just because I forget to add @Transactional annotation on the method that calls em.persist(myNewEntity) as I thought all the callers will be on Transaction,

the error is not popping directly but when I tried to delete all rows from a table

really the error is misleading but its the same as the second case in @axtavt answer

so

make sure that save operation is done within an active transaction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文