HHH-16414 Improve TableGroup resolution for get or create
This commit is contained in:
parent
bc31a9532a
commit
288242a10f
|
@ -9,14 +9,11 @@ package org.hibernate.sql.ast.spi;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.hibernate.metamodel.mapping.ModelPartContainer;
|
|
||||||
import org.hibernate.metamodel.model.domain.NavigableRole;
|
import org.hibernate.metamodel.model.domain.NavigableRole;
|
||||||
import org.hibernate.spi.NavigablePath;
|
import org.hibernate.spi.NavigablePath;
|
||||||
import org.hibernate.sql.ast.SqlTreeCreationLogger;
|
import org.hibernate.sql.ast.SqlTreeCreationLogger;
|
||||||
import org.hibernate.sql.ast.tree.from.CorrelatedTableGroup;
|
import org.hibernate.sql.ast.tree.from.CorrelatedTableGroup;
|
||||||
import org.hibernate.sql.ast.tree.from.PluralTableGroup;
|
|
||||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||||
import org.hibernate.sql.ast.tree.from.VirtualTableGroup;
|
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
|
@ -54,19 +51,27 @@ public class SimpleFromClauseAccessImpl implements FromClauseAccess {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableGroup findTableGroupForGetOrCreate(NavigablePath navigablePath) {
|
public TableGroup findTableGroupForGetOrCreate(NavigablePath navigablePath) {
|
||||||
final TableGroup tableGroup = findTableGroup( navigablePath );
|
final TableGroup localTableGroup = tableGroupMap.get( navigablePath );
|
||||||
if ( parent != null && tableGroup != null && navigablePath.getParent() != null ) {
|
if ( localTableGroup != null || parent == null ) {
|
||||||
|
return localTableGroup;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
final TableGroup tableGroup = parent.findTableGroup( navigablePath );
|
||||||
|
if ( tableGroup != null && navigablePath.getParent() != null ) {
|
||||||
final NavigableRole navigableRole = tableGroup.getModelPart().getNavigableRole();
|
final NavigableRole navigableRole = tableGroup.getModelPart().getNavigableRole();
|
||||||
if ( navigableRole != null && navigableRole.getParent() != null ) {
|
if ( navigableRole.getParent() != null ) {
|
||||||
// Traverse up the navigable path to the point where resolving the path leads us to a regular TableGroup
|
// Traverse up the navigable path to the point where resolving the path leads us to the parent TableGroup
|
||||||
NavigableRole parentRole = navigableRole.getParent();
|
NavigableRole parentRole = navigableRole.getParent();
|
||||||
NavigablePath parentPath = navigablePath.getParent();
|
NavigablePath parentPath = navigablePath.getParent();
|
||||||
while ( parentRole.getParent() != null ) {
|
while ( parentRole.getParent() != null ) {
|
||||||
parentRole = parentRole.getParent();
|
parentRole = parentRole.getParent();
|
||||||
parentPath = parentPath.getParent();
|
parentPath = parentPath.getParent();
|
||||||
}
|
}
|
||||||
// Only return the TableGroup if its regular parent TableGroup corresponds to the underlying one
|
final TableGroup parentFound = parent.findTableGroup( parentPath );
|
||||||
if ( getUnderlyingTableGroup( findTableGroup( parentPath ) ) == getUnderlyingTableGroup( tableGroup ) ) {
|
// Only return the TableGroup if there's no corresponding group in the parent FromClauseAccess
|
||||||
|
// or if there is one, but it's the same or correlated to the locally found one.
|
||||||
|
if ( parentFound == null || getCorrelatedTableGroup( parentFound ) == getCorrelatedTableGroup(
|
||||||
|
tableGroupMap.get( parentPath ) ) ) {
|
||||||
return tableGroup;
|
return tableGroup;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -76,16 +81,11 @@ public class SimpleFromClauseAccessImpl implements FromClauseAccess {
|
||||||
}
|
}
|
||||||
return tableGroup;
|
return tableGroup;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private TableGroup getUnderlyingTableGroup(TableGroup tableGroup) {
|
private TableGroup getCorrelatedTableGroup(TableGroup tableGroup) {
|
||||||
if ( tableGroup instanceof VirtualTableGroup ) {
|
if ( tableGroup instanceof CorrelatedTableGroup ) {
|
||||||
return getUnderlyingTableGroup( ( (VirtualTableGroup) tableGroup ).getUnderlyingTableGroup() );
|
return getCorrelatedTableGroup( ( (CorrelatedTableGroup) tableGroup ).getCorrelatedTableGroup() );
|
||||||
}
|
|
||||||
else if ( tableGroup instanceof CorrelatedTableGroup ) {
|
|
||||||
return getUnderlyingTableGroup( ( (CorrelatedTableGroup) tableGroup ).getCorrelatedTableGroup() );
|
|
||||||
}
|
|
||||||
else if ( tableGroup instanceof PluralTableGroup ) {
|
|
||||||
return ( (PluralTableGroup) tableGroup ).getElementTableGroup();
|
|
||||||
}
|
}
|
||||||
return tableGroup;
|
return tableGroup;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue