diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java index 4c8f96b770..9c07e3f292 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java @@ -887,7 +887,6 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister { return temp; } - public String fromTableFragment(String alias) { return getTableName() + ' ' + alias; } @@ -902,11 +901,24 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister { select.setExtraSelectList( discriminatorFragment( name ), getDiscriminatorAlias() ); } else { + if ( getEntityMetamodel().getSuperclass() != null ) { + name = generateTableAlias( name, getRootHierarchyClassTableIndex() ); + } select.addColumn( name, explicitDiscriminatorColumnName, discriminatorAlias ); } } } + private int getRootHierarchyClassTableIndex() { + final String rootHierarchyClassTableName = naturalOrderTableNames[0]; + for ( int i = 0; i < subclassTableNameClosure.length; i++ ) { + if ( subclassTableNameClosure[i].equals( rootHierarchyClassTableName ) ) { + return i; + } + } + return 0; + } + private CaseFragment discriminatorFragment(String alias) { CaseFragment cases = getFactory().getDialect().createCaseFragment();