HHH-15713 UnknownTableReferenceException on @ElementCollection of @Embeddable containing a @MayToOne with a @ManyToMany

This commit is contained in:
Andrea Boriero 2022-11-23 19:10:01 +01:00 committed by Andrea Boriero
parent 1b417126bf
commit 0d20cea0b3
4 changed files with 48 additions and 18 deletions

View File

@ -19,6 +19,7 @@ import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.exec.spi.JdbcParameterBindings; import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.entity.LoadingEntityEntry; import org.hibernate.sql.results.graph.entity.LoadingEntityEntry;
/** /**
@ -104,7 +105,7 @@ public class SubselectFetch {
return new StandardRegistrationHandler( return new StandardRegistrationHandler(
batchFetchQueue, batchFetchQueue,
sqlAst.getQuerySpec(), sqlAst,
tableGroup, tableGroup,
jdbcParameters, jdbcParameters,
jdbcParameterBindings jdbcParameterBindings
@ -137,7 +138,7 @@ public class SubselectFetch {
public static class StandardRegistrationHandler implements RegistrationHandler { public static class StandardRegistrationHandler implements RegistrationHandler {
private final BatchFetchQueue batchFetchQueue; private final BatchFetchQueue batchFetchQueue;
private final QuerySpec loadingSqlAst; private final SelectStatement loadingSqlAst;
private final TableGroup ownerTableGroup; private final TableGroup ownerTableGroup;
private final List<JdbcParameter> loadingJdbcParameters; private final List<JdbcParameter> loadingJdbcParameters;
private final JdbcParameterBindings loadingJdbcParameterBindings; private final JdbcParameterBindings loadingJdbcParameterBindings;
@ -145,7 +146,7 @@ public class SubselectFetch {
private StandardRegistrationHandler( private StandardRegistrationHandler(
BatchFetchQueue batchFetchQueue, BatchFetchQueue batchFetchQueue,
QuerySpec loadingSqlAst, SelectStatement loadingSqlAst,
TableGroup ownerTableGroup, TableGroup ownerTableGroup,
List<JdbcParameter> loadingJdbcParameters, List<JdbcParameter> loadingJdbcParameters,
JdbcParameterBindings loadingJdbcParameterBindings) { JdbcParameterBindings loadingJdbcParameterBindings) {
@ -160,11 +161,13 @@ public class SubselectFetch {
if ( !entry.getDescriptor().hasSubselectLoadableCollections() ) { if ( !entry.getDescriptor().hasSubselectLoadableCollections() ) {
return; return;
} }
if ( shouldAddSubselectFetch( entry ) ) {
final SubselectFetch subselectFetch = subselectFetches.computeIfAbsent( final SubselectFetch subselectFetch = subselectFetches.computeIfAbsent(
entry.getEntityInitializer().getNavigablePath(), entry.getEntityInitializer().getNavigablePath(),
navigablePath -> new SubselectFetch( navigablePath -> new SubselectFetch(
null, null,
loadingSqlAst, loadingSqlAst.getQuerySpec(),
ownerTableGroup, ownerTableGroup,
loadingJdbcParameters, loadingJdbcParameters,
loadingJdbcParameterBindings, loadingJdbcParameterBindings,
@ -175,4 +178,22 @@ public class SubselectFetch {
batchFetchQueue.addSubselect( key, subselectFetch ); batchFetchQueue.addSubselect( key, subselectFetch );
} }
} }
private boolean shouldAddSubselectFetch(LoadingEntityEntry entry) {
if ( entry.getEntityInitializer().isEntityResultInitializer() ) {
return true;
}
final NavigablePath entityInitializerParent = entry.getEntityInitializer().getNavigablePath().getParent();
// We want to add only the collections of the loading entities
for ( DomainResult domainResult : loadingSqlAst.getDomainResultDescriptors() ) {
if ( domainResult.getNavigablePath().equals( entityInitializerParent ) ) {
return true;
}
}
return false;
}
}
} }

View File

@ -464,7 +464,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
.getEntityInstance(); .getEntityInstance();
if ( proxy != null && ( proxy instanceof MapProxy if ( proxy != null && ( proxy instanceof MapProxy
|| entityDescriptor.getJavaType().getJavaTypeClass().isInstance( proxy ) ) ) { || entityDescriptor.getJavaType().getJavaTypeClass().isInstance( proxy ) ) ) {
if ( this instanceof EntityResultInitializer && entityInstanceFromExecutionContext != null ) { if ( this.isEntityResultInitializer() && entityInstanceFromExecutionContext != null ) {
this.entityInstance = entityInstanceFromExecutionContext; this.entityInstance = entityInstanceFromExecutionContext;
registerLoadingEntity( rowProcessingState, entityInstance ); registerLoadingEntity( rowProcessingState, entityInstance );
} }
@ -477,7 +477,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
if ( existingEntity != null ) { if ( existingEntity != null ) {
this.entityInstance = existingEntity; this.entityInstance = existingEntity;
} }
else if ( this instanceof EntityResultInitializer && entityInstanceFromExecutionContext != null ) { else if ( this.isEntityResultInitializer() && entityInstanceFromExecutionContext != null ) {
this.entityInstance = entityInstanceFromExecutionContext; this.entityInstance = entityInstanceFromExecutionContext;
registerLoadingEntity( rowProcessingState, entityInstance ); registerLoadingEntity( rowProcessingState, entityInstance );
} }

View File

@ -60,4 +60,8 @@ public interface EntityInitializer extends FetchParentAccess {
return this; return this;
} }
default boolean isEntityResultInitializer() {
return false;
}
} }

View File

@ -56,4 +56,9 @@ public class EntityResultInitializer extends AbstractEntityInitializer {
public String toString() { public String toString() {
return CONCRETE_NAME + "(" + getNavigablePath() + ")"; return CONCRETE_NAME + "(" + getNavigablePath() + ")";
} }
@Override
public boolean isEntityResultInitializer() {
return true;
}
} }