From 7cd6d0422f7ca0160cc12ed2bd7c0f141c4d2506 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 17 Jan 2023 15:49:31 +0100 Subject: [PATCH] HHH-16023 Check fetchable declaring type when not found in persister --- .../entity/AbstractEntityPersister.java | 23 +++++++++++++++---- .../ql/NamedNativeQueryWithGenericsTest.java | 4 ++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 5043acc2ed..ff9f965723 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -1828,7 +1828,7 @@ public abstract class AbstractEntityPersister return fetches.build(); } - private boolean isSelectable(FetchParent fetchParent, Fetchable fetchable) { + protected boolean isSelectable(FetchParent fetchParent, Fetchable fetchable) { if ( fetchParent instanceof EmbeddableResultGraphNode ) { return true; } @@ -1836,9 +1836,24 @@ public abstract class AbstractEntityPersister final AttributeMapping attributeMapping = fetchable.asAttributeMapping(); if ( attributeMapping != null ) { final int propertyNumber = attributeMapping.getStateArrayPosition(); -// final int tableNumber = getSubclassPropertyTableNumber( propertyNumber ); -// return !isSubclassTableSequentialSelect( tableNumber ) && propertySelectable[propertyNumber]; - return propertySelectable[propertyNumber]; + if ( propertyNumber < propertySelectable.length ) { + return propertySelectable[propertyNumber]; + } + else { + final ManagedMappingType declaringType = attributeMapping.getDeclaringType(); + // try to check select-ability from the declaring type + if ( declaringType != this ) { + assert declaringType instanceof AbstractEntityPersister; + final AbstractEntityPersister subPersister = (AbstractEntityPersister) declaringType; + return subPersister.propertySelectable[propertyNumber]; + } + else { + throw new IllegalArgumentException( + "Unrecognized fetchable [" + fetchable.getFetchableName() + "] at index " + + propertyNumber + " for entity [" + getEntityName() + "]" + ); + } + } } else { return true; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ql/NamedNativeQueryWithGenericsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ql/NamedNativeQueryWithGenericsTest.java index fdcb8e53fe..63467427ba 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ql/NamedNativeQueryWithGenericsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ql/NamedNativeQueryWithGenericsTest.java @@ -8,8 +8,11 @@ package org.hibernate.orm.test.jpa.ql; import java.util.List; +import org.hibernate.dialect.H2Dialect; + import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; @@ -38,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; }) @SessionFactory @JiraKey("HHH-16023") +@RequiresDialect(H2Dialect.class) public class NamedNativeQueryWithGenericsTest { @Test public void testNamedNativeQuery(SessionFactoryScope scope) {