Hibernate java.lang.ClassCastException: org.hibernate.action.EntityIdentityInsertAction cannot be cast to org.hibernate.action.EntityInsertAction
Asked Answered
P

2

6

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?

Psychobiology answered 14/9, 2011 at 6:41 Comment(0)
N
2

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.

Niobic answered 14/9, 2011 at 9:33 Comment(0)
M
0

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

Mulley answered 6/3, 2019 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.