Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
package org.hibernate.persister.entity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.hibernate.HibernateException;
import org.hibernate.Internal;
Expand Down Expand Up @@ -349,10 +351,10 @@ public SingleTableEntityPersister(
);

// SUBCLASSES
final List<Subclass> subclasses = persistentClass.getSubclasses();
final List<Subclass> subclasses = persistentClass.getSubclasses().stream().sorted(Comparator.comparing(PersistentClass::getEntityName)).collect(Collectors.toList());
Copy link
Author

@Stephen-Allen Stephen-Allen Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subclasses need to be ordered by the entity name to match the TreeMap with entity name keys used for AbstractEntityPersister.subclassMappingTypes. Otherwise the column orders can get switched up incorrectly and return the wrong values.

for ( int k = 0; k < subclasses.size(); k++ ) {
Subclass subclass = subclasses.get( k );
subclassClosure[k] = subclass.getEntityName();
subclassClosure[k+1] = subclass.getEntityName();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This off by one error was accidentally introduced in commit a7da407 during a refactor from a foreach loop to a for loop.

Object subclassDiscriminatorValue = DiscriminatorHelper.getDiscriminatorValue( subclass );
addSubclassByDiscriminatorValue(
subclassesByDiscriminatorValueLocal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import java.util.function.Function;

import org.hibernate.metamodel.mapping.DiscriminatorMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.SelectableMapping;
import org.hibernate.metamodel.mapping.SelectablePath;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.NestedColumnReference;
import org.hibernate.sql.ast.tree.from.EmbeddableFunctionTableReference;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.type.NullType;
Expand Down Expand Up @@ -90,7 +90,10 @@ static ColumnReferenceKey createColumnReferenceKey(String tableExpression, Selec
static ColumnReferenceKey createColumnReferenceKey(TableReference tableReference, SelectableMapping selectable) {
assert tableReference.containsAffectedTableName( selectable.getContainingTableExpression() )
: String.format( ROOT, "Expecting tables to match between TableReference (%s) and SelectableMapping (%s)", tableReference.getTableId(), selectable.getContainingTableExpression() );
return createColumnReferenceKey( tableReference, selectable.getSelectablePath(), selectable.getJdbcMapping() );
final JdbcMapping jdbcMapping = (selectable instanceof DiscriminatorMapping) ?
((DiscriminatorMapping)selectable).getUnderlyingJdbcMapping() :
selectable.getJdbcMapping();
return createColumnReferenceKey( tableReference, selectable.getSelectablePath(), jdbcMapping );
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the discriminator case by using the underlying JDBC type as part of the key to look up the discriminator property in the sqlSelectionMap in DomainResultCreationStateImpl.resolveSqlExpression(ColumnReferenceKey key, Function<SqlAstProcessingState, Expression> creator) function.

}

default Expression resolveSqlExpression(TableReference tableReference, SelectableMapping selectableMapping) {
Expand Down
Loading
Loading