Hello,
I’m migrating from Hibernate 5. to 6.2 and running into an error (full stacktrace below):
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [MyEntity#IdOfTheEntity]
The entity with ID IdOfTheEntity
does exist but it is not of class MyEntity
.
I’ve looked into what is happening and what I think is the problem is that in my mapping some columns were reused in related (same parent class and same table) classes.
Here’s a simplified example to illustrate; note that the food_id
column is reused in multiple classes but it is not in the parent Container
class:
@BatchSize(size = 512)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type")
@DiscriminatorValue(value = "CONTAINER")
class Container {
}
@DiscriminatorValue("CHEESE_CONTAINER")
class CheeseContainer extends Container {
@Column(name = "food_id")
Cheese cheese;
}
@DiscriminatorValue("FRUIT_CONTAINER")
class FruitContainer extends Container {
@Column(name = "food_id")
Fruit fruit;
}
@BatchSize(size = 512)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type")
@DiscriminatorValue(value = "FOOD")
class Food {
}
@DiscriminatorValue("CHEESE")
class Cheese extends Food {
}
@DiscriminatorValue("FRUIT")
class Fruit extends Food {
}
This worked fine in Hibernate 5 but fails when upgrading to 6.2
The errors happens randomly, most probably depending on the loading order of the entities and when the batch loading kicks in.
What seems to be happening is that when doing a batch load, Hibernate decides that the column will contain classes of one of the types, but here there might be multiple types on the same column.
I realize that it might be a design error to reuse a column, but I’m not sure if it falls in the “not supported” category or if it should work.
Can you please advise? Thanks in advance
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [MyEntity#IdOfTheEntity]
at org.hibernate.boot.internal.StandardEntityNotFoundDelegate.handleEntityNotFound(StandardEntityNotFoundDelegate.java:26)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:207)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:241)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:106)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:78)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:138)
at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1231)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1075)
at org.hibernate.sql.results.graph.entity.internal.BatchInitializeEntitySelectFetchInitializer.resolveInstance(BatchInitializeEntitySelectFetchInitializer.java:74)
at org.hibernate.sql.results.internal.InitializersList.resolveInstances(InitializersList.java:88)
at org.hibernate.sql.results.internal.StandardRowReader.coordinateInitializers(StandardRowReader.java:110)
at org.hibernate.sql.results.internal.StandardRowReader.readRow(StandardRowReader.java:87)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:179)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:362)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:168)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:93)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31)
at org.hibernate.loader.ast.internal.SingleIdEntityLoaderDynamicBatch.load(SingleIdEntityLoaderDynamicBatch.java:140)
at org.hibernate.loader.ast.internal.SingleIdEntityLoaderDynamicBatch.load(SingleIdEntityLoaderDynamicBatch.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.doLoad(AbstractEntityPersister.java:3367)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3357)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:602)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromCacheOrDatasource(DefaultLoadEventListener.java:588)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:557)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:550)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:202)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:101)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:78)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:138)
at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1231)
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1043)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:173)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:44)
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:102)
at management.business.instrument.Instrument$HibernateProxy$P9DCEOxm.hashCode(Unknown Source)
at management.business.PositionKey.hashCode(PositionKey.java:65)
at management.business.Position.hashCode(Position.java:124)
at java.base/java.util.HashMap.hash(HashMap.java:338)
at java.base/java.util.HashMap.put(HashMap.java:610)
at java.base/java.util.HashSet.add(HashSet.java:221)
at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336)
at org.hibernate.collection.spi.PersistentSet.injectLoadedState(PersistentSet.java:335)
at org.hibernate.loader.ast.internal.CollectionLoaderNamedQuery.load(CollectionLoaderNamedQuery.java:64)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:668)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1697)
at org.hibernate.collection.spi.AbstractPersistentCollection.lambda$initialize$3(AbstractPersistentCollection.java:617)
at org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at org.hibernate.collection.spi.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:615)
at org.hibernate.collection.spi.AbstractPersistentCollection.read(AbstractPersistentCollection.java:136)
at org.hibernate.collection.spi.AbstractPersistentCollection.lambda$readSize$0(AbstractPersistentCollection.java:163)
at org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at org.hibernate.collection.spi.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.spi.PersistentSet.size(PersistentSet.java:151)
at util.HibernateTest.main(HibernateTest.java:35)