|
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:
for (int i = 0; i < 10000; i++) {
final Entity1 entity1 = new Entity1();
session.persist(entity1);
final Entity2 entity2 = new Entity2();
session.persist(entity2);
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.
|