org.hibernate.WrongClassException after setting default_batch_fetch_size

Description

In order optimize the load time of our entities, we set the default_batch_fetch_size to 256 (previously the value was not set at all). Now during a specific operation in which we load a set of entities, some of them with eager loading and some of them with lazy loading, we get the exception from below. The data model is the following:
A - entity
B1 and B2 entities where B2 extends B1 and both B1 and B2 are stored in same table but differentiated by DTYPE
C - entity
Entity A contains a list of B1 and B2 entities (eager loading) and only B2 contains a list of C entities which are lazy loaded.
The exception happens when loading all A entities at once and going through B1/B2 list and trying to access first entity from list of C entities (which for that specific case are loaded lazily).
When debugged, the object referenced in exception is totally unrelated to the entity accessed.

The stacktrace was generated using Hibernate 4.3.5, however the issue is also reproducible on 4.3.11

org.hibernate.WrongClassException: Object [id=4010] was not of the specified subclass [B2] : loaded object was of wrong class class B1
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1550)
at org.hibernate.loader.Loader.getRow(Loader.java:1503)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324)
at org.hibernate.loader.Loader.loadCollectionBatch(Loader.java:2299)
at org.hibernate.loader.collection.plan.LegacyBatchingCollectionInitializerBuilder$LegacyBatchingCollectionInitializer.initialize(LegacyBatchingCollectionInitializerBuilder.java:100)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1897)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
at org.hibernate.collection.internal.PersistentBag.get(PersistentBag.java:466)

Environment

Hibernate 4.3.5 and 4.3.11, Ubuntu Desktop 14.04, database MySQL

Activity

Show:
Gail Badner
June 14, 2016, 9:01 PM

4.3 is no longer maintained. Does this happen with 5.2? If so, please attach a runnable test case that reproduces your issue. The test code at https://github.com/hibernate/hibernate-test-case-templates can be used to create a runnable test case.

Sergiu Hlihor
June 15, 2016, 6:53 PM

We cannot migrate yet on 5.2 due to HHH-10820.

Gail Badner
June 23, 2016, 12:23 AM

is fixed. Please attach a runnable test case that reproduces your issue. The test code at https://github.com/hibernate/hibernate-test-case-templates can be used to create a runnable test case.

Sergiu Hlihor
July 29, 2016, 8:34 AM

The issue can be reproduced also with 5.2.1. I'll comeback soon with a test case.

Assignee

Unassigned

Reporter

Sergiu Hlihor

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure