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.SelectStatement;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.entity.LoadingEntityEntry;
/**
@ -104,7 +105,7 @@ public class SubselectFetch {
return new StandardRegistrationHandler(
batchFetchQueue,
sqlAst.getQuerySpec(),
sqlAst,
tableGroup,
jdbcParameters,
jdbcParameterBindings
@ -137,7 +138,7 @@ public class SubselectFetch {
public static class StandardRegistrationHandler implements RegistrationHandler {
private final BatchFetchQueue batchFetchQueue;
private final QuerySpec loadingSqlAst;
private final SelectStatement loadingSqlAst;
private final TableGroup ownerTableGroup;
private final List<JdbcParameter> loadingJdbcParameters;
private final JdbcParameterBindings loadingJdbcParameterBindings;
@ -145,7 +146,7 @@ public class SubselectFetch {
private StandardRegistrationHandler(
BatchFetchQueue batchFetchQueue,
QuerySpec loadingSqlAst,
SelectStatement loadingSqlAst,
TableGroup ownerTableGroup,
List<JdbcParameter> loadingJdbcParameters,
JdbcParameterBindings loadingJdbcParameterBindings) {
@ -160,11 +161,13 @@ public class SubselectFetch {
if ( !entry.getDescriptor().hasSubselectLoadableCollections() ) {
return;
}
if ( shouldAddSubselectFetch( entry ) ) {
final SubselectFetch subselectFetch = subselectFetches.computeIfAbsent(
entry.getEntityInitializer().getNavigablePath(),
navigablePath -> new SubselectFetch(
null,
loadingSqlAst,
loadingSqlAst.getQuerySpec(),
ownerTableGroup,
loadingJdbcParameters,
loadingJdbcParameterBindings,
@ -175,4 +178,22 @@ public class 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();
if ( proxy != null && ( proxy instanceof MapProxy
|| entityDescriptor.getJavaType().getJavaTypeClass().isInstance( proxy ) ) ) {
if ( this instanceof EntityResultInitializer && entityInstanceFromExecutionContext != null ) {
if ( this.isEntityResultInitializer() && entityInstanceFromExecutionContext != null ) {
this.entityInstance = entityInstanceFromExecutionContext;
registerLoadingEntity( rowProcessingState, entityInstance );
}
@ -477,7 +477,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
if ( existingEntity != null ) {
this.entityInstance = existingEntity;
}
else if ( this instanceof EntityResultInitializer && entityInstanceFromExecutionContext != null ) {
else if ( this.isEntityResultInitializer() && entityInstanceFromExecutionContext != null ) {
this.entityInstance = entityInstanceFromExecutionContext;
registerLoadingEntity( rowProcessingState, entityInstance );
}

View File

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

View File

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