We're updating the issue view to help you get more done. 

ClassCastException when bulk inserting entities

Description

When bulk inserting entities, you might get an error similar to this:

Exception in thread "main" java.lang.ClassCastException: org.hibernate.action.internal.DelayedPostInsertIdentifier cannot be cast to java.lang.Long
at java.lang.Long.compareTo(Long.java:54)
at org.hibernate.internal.util.compare.ComparableComparator.compare(ComparableComparator.java:41)
at org.hibernate.internal.util.compare.ComparableComparator.compare(ComparableComparator.java:36)
at org.hibernate.type.AbstractStandardBasicType.compare(AbstractStandardBasicType.java:225)
at org.hibernate.action.internal.CollectionAction.compareTo(CollectionAction.java:172)
at org.hibernate.engine.spi.ExecutableList.add(ExecutableList.java:222)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:250)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:274)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:103)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)

This is better illustrated in the attached sample application.

This is the main logics causing problems:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 for (int i = 0; i < 10000; i++) { final Entity1 entity1 = new Entity1(); session.persist(entity1); final Entity2 entity2 = new Entity2(); session.persist(entity2); // session.flush(); final Criteria criteria = session.createCriteria(Entity2.class); entity1.setEntities(criteria.list()); if (i % BULK_SIZE == 0) { session.flush(); } }

If you uncomment the first flush operation, it will work, but at a highly reduced performance level.

If you comment out the collection setter, you'll see that the application finishes successfully in a few seconds.

Environment

None

Status

Assignee

Unassigned

Reporter

Stig Runar Vangen

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

4.3.6

Priority

Major