diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElement.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElement.java index d1a82f6630..263b4facd2 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElement.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElement.java @@ -341,14 +341,7 @@ public class FromElement extends HqlSqlWalkerNode implements DisplayableNode, Pa throw new IllegalStateException( "No table alias for node " + this ); } - final String propertyName; - if ( getEntityPersister() != null && getEntityPersister().getEntityMetamodel() != null - && getEntityPersister().getEntityMetamodel().hasNonIdentifierPropertyNamedId() ) { - propertyName = getEntityPersister().getIdentifierPropertyName(); - } - else { - propertyName = EntityPersister.ENTITY_ID; - } + final String propertyName = getIdentifierPropertyName(); if ( getWalker().getStatementType() == HqlSqlTokenTypes.SELECT ) { return getPropertyMapping( propertyName ).toColumns( table, propertyName ); @@ -536,6 +529,10 @@ public class FromElement extends HqlSqlWalkerNode implements DisplayableNode, Pa return elementType.getCollectionPropertyReference( propertyName ); } + public String getIdentifierPropertyName() { + return elementType.getIdentifierPropertyName(); + } + public void setFetch(boolean fetch) { this.fetch = fetch; // Fetch can't be used with scroll() or iterate(). diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElementType.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElementType.java index 63ce831e96..66ed2e54f9 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElementType.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElementType.java @@ -129,10 +129,10 @@ class FromElementType { */ String renderScalarIdentifierSelect(int i) { checkInitialized(); - String[] cols = getPropertyMapping( EntityPersister.ENTITY_ID ).toColumns( - getTableAlias(), - EntityPersister.ENTITY_ID - ); + + final String idPropertyName = getIdentifierPropertyName(); + String[] cols = getPropertyMapping( idPropertyName ).toColumns( getTableAlias(), idPropertyName ); + StringBuilder buf = new StringBuilder(); // For property references generate . as for ( int j = 0; j < cols.length; j++ ) { @@ -677,4 +677,14 @@ class FromElementType { return queryableCollection.toColumns( propertyName ); } } + + public String getIdentifierPropertyName() { + if ( getEntityPersister() != null && getEntityPersister().getEntityMetamodel() != null + && getEntityPersister().getEntityMetamodel().hasNonIdentifierPropertyNamedId() ) { + return getEntityPersister().getIdentifierPropertyName(); + } + else { + return EntityPersister.ENTITY_ID; + } + } }