Given:
* Single table inheritance using discriminator * One child entity has an OneToMany relationship to a certain entity of type A using FetchMode.*_SUBSELECT_* * Another child entity has another OneToMany relationship to a certain entity of type B also using FetchMode.*_SUBSELECT_*
When
* Querying both entities and initializing the first child relationship
Then
* A NPE happens when hibernate wrongly tries to load the 2nd child relationship with entity of type A, which mapping doesn’t exist for the second child type.
{code:java}@Entity @Table(name = "MY_ENTITY") @DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER) @DiscriminatorValue("0") public class ParentEntity {
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
}{code}
{code:java}@Entity @DiscriminatorValue("1") public class MyEntityWithAnotherEntities extends ParentEntity { @OneToMany(mappedBy = "myEntity") @Fetch(FetchMode.SUBSELECT) List<AnotherEntity> anotherEntities = new ArrayList<>(); }{code}
{code:java}@Entity @DiscriminatorValue("2") public class MyEntityWithSomeOtherEntities extends ParentEntity { @OneToMany(mappedBy = "myEntity") @Fetch(FetchMode.SUBSELECT) List<SomeOtherEntity> someOtherEntitiesEntities = new ArrayList<>(); }{code}
{code:java} @Test public void hhhXXXXTest() throws Exception { // BaseCoreFunctionalTestCase automatically creates the SessionFactory and // provides the Session. AnotherEntity anotherEntity1 = new AnotherEntity(); MyEntityWithAnotherEntities child1 = new MyEntityWithAnotherEntities(); MyEntityWithSomeOtherEntities child2 = new MyEntityWithSomeOtherEntities(); try (Session s = openSession()) { Transaction tx = s.beginTransaction();
child1.anotherEntities.add(anotherEntity1); anotherEntity1.myEntity = child1;
s.persist(child1); s.persist(child2); s.persist(anotherEntity1); tx.commit(); }
try (Session s = openSession()) { Transaction tx = s.beginTransaction(); Query<ParentEntity> query = s.createQuery("from ParentEntity e order by e.id", ParentEntity.class); List<ParentEntity> entities = query.list();
assertThat(entities).hasSize(2); assertThat(entities.get(0)).isInstanceOf(MyEntityWithAnotherEntities.class);
// initialize anotherEntities which uses subselect batch fetch which will lead // to the exception assertThat(((MyEntityWithAnotherEntities) entities.get(0)).anotherEntities).isNotEmpty();
tx.commit(); } }{code}
{noformat}java.lang.NullPointerException: Cannot invoke "org.hibernate.collection.spi.PersistentCollection.beginRead()" because "containedCollection" is null at org.hibernate.loader.ast.internal.CollectionLoaderSubSelectFetch.load(CollectionLoaderSubSelectFetch.java:110) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:716) 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.PersistentBag.isEmpty(PersistentBag.java:359) at org.assertj.core.util.IterableUtil.isNullOrEmpty(IterableUtil.java:37) at org.assertj.core.internal.Iterables.assertNotEmpty(Iterables.java:188) at org.assertj.core.api.AbstractIterableAssert.isNotEmpty(AbstractIterableAssert.java:142) at org.hibernate.bugs.ORMUnitTestCase.hhh16254Test(ORMUnitTestCase.java:88) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:833)
{noformat}
The issue happens because SubselectFetch.+StandardRegistrationHandler+.addKey method wrongly registers a key even for the child entity which doesn’t have that collection mapped.
This works fine on latest hibernate 5 series. It doesn’t work in any hibernate 6 series.
Failing test scenario to be attached and also available here [https://github.com/ratoaq2/HHH-16258|https://github.com/ratoaq2/HHH-16258|smart-link] |
|