HHH-15418 Composite primary key with NativeQuery & alias not working

This commit is contained in:
Andrea Boriero 2022-08-02 19:13:08 +02:00 committed by Andrea Boriero
parent 89d99bfdbd
commit 4a7c58b983
4 changed files with 45 additions and 6 deletions

View File

@ -244,6 +244,7 @@ import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.graph.embeddable.EmbeddableResultGraphNode;
import org.hibernate.sql.results.graph.entity.internal.EntityResultImpl; import org.hibernate.sql.results.graph.entity.internal.EntityResultImpl;
import org.hibernate.sql.results.internal.SqlSelectionImpl; import org.hibernate.sql.results.internal.SqlSelectionImpl;
import org.hibernate.stat.spi.StatisticsImplementor; import org.hibernate.stat.spi.StatisticsImplementor;
@ -1867,10 +1868,15 @@ public abstract class AbstractEntityPersister
FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming(); FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
final boolean selectable; final boolean selectable;
if ( fetchable instanceof AttributeMapping ) { if ( fetchable instanceof AttributeMapping ) {
final int propertyNumber = ( (AttributeMapping) fetchable ).getStateArrayPosition(); if ( fetchParent instanceof EmbeddableResultGraphNode && ( (EmbeddableResultGraphNode) fetchParent ).getReferencedMappingContainer() == getIdentifierMapping() ) {
final int tableNumber = getSubclassPropertyTableNumber( propertyNumber ); selectable = true;
selectable = !isSubclassTableSequentialSelect( tableNumber ) }
&& propertySelectable[propertyNumber]; else {
final int propertyNumber = ( (AttributeMapping) fetchable ).getStateArrayPosition();
final int tableNumber = getSubclassPropertyTableNumber( propertyNumber );
selectable = !isSubclassTableSequentialSelect( tableNumber )
&& propertySelectable[propertyNumber];
}
} }
else { else {
selectable = true; selectable = true;

View File

@ -257,7 +257,7 @@ public class Builders {
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
if ( fetchable instanceof BasicValuedModelPart ) { if ( fetchable instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicValuedFetchable = (BasicValuedModelPart) fetchable; final BasicValuedModelPart basicValuedFetchable = (BasicValuedModelPart) fetchable;
return new ImplicitFetchBuilderBasic( fetchPath, basicValuedFetchable ); return new ImplicitFetchBuilderBasic( fetchPath, basicValuedFetchable, creationState );
} }
if ( fetchable instanceof EmbeddableValuedFetchable ) { if ( fetchable instanceof EmbeddableValuedFetchable ) {

View File

@ -29,7 +29,8 @@ import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata;
public interface FetchBuilder { public interface FetchBuilder {
Fetch buildFetch( Fetch buildFetch(
FetchParent parent, FetchParent parent,
NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, NavigablePath fetchPath,
JdbcValuesMetadata jdbcResultsMetadata,
BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver,
DomainResultCreationState domainResultCreationState); DomainResultCreationState domainResultCreationState);

View File

@ -6,7 +6,9 @@
*/ */
package org.hibernate.query.results.implicit; package org.hibernate.query.results.implicit;
import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.BasicValuedModelPart; import org.hibernate.metamodel.mapping.BasicValuedModelPart;
@ -27,6 +29,7 @@ import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.basic.BasicFetch; import org.hibernate.sql.results.graph.basic.BasicFetch;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata; import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata;
import static org.hibernate.query.results.ResultsHelper.impl;
import static org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition; import static org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition;
import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey; import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey;
@ -36,10 +39,23 @@ import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnRefere
public class ImplicitFetchBuilderBasic implements ImplicitFetchBuilder, BasicValuedFetchBuilder { public class ImplicitFetchBuilderBasic implements ImplicitFetchBuilder, BasicValuedFetchBuilder {
private final NavigablePath fetchPath; private final NavigablePath fetchPath;
private final BasicValuedModelPart fetchable; private final BasicValuedModelPart fetchable;
private final FetchBuilder fetchBuilder;
public ImplicitFetchBuilderBasic(NavigablePath fetchPath, BasicValuedModelPart fetchable) { public ImplicitFetchBuilderBasic(NavigablePath fetchPath, BasicValuedModelPart fetchable) {
this.fetchPath = fetchPath; this.fetchPath = fetchPath;
this.fetchable = fetchable; this.fetchable = fetchable;
this.fetchBuilder = null;
}
public ImplicitFetchBuilderBasic(
NavigablePath fetchPath,
BasicValuedModelPart fetchable,
DomainResultCreationState creationState) {
this.fetchPath = fetchPath;
this.fetchable = fetchable;
final DomainResultCreationStateImpl creationStateImpl = impl( creationState );
final Function<String, FetchBuilder> fetchBuilderResolver = creationStateImpl.getCurrentExplicitFetchMementoResolver();
this.fetchBuilder = fetchBuilderResolver.apply( fetchable.getFetchableName() );
} }
@Override @Override
@ -54,6 +70,15 @@ public class ImplicitFetchBuilderBasic implements ImplicitFetchBuilder, BasicVal
JdbcValuesMetadata jdbcResultsMetadata, JdbcValuesMetadata jdbcResultsMetadata,
BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver,
DomainResultCreationState domainResultCreationState) { DomainResultCreationState domainResultCreationState) {
if ( fetchBuilder != null ) {
return (BasicFetch<?>) fetchBuilder.buildFetch(
parent,
fetchPath,
jdbcResultsMetadata,
legacyFetchResolver,
domainResultCreationState
);
}
final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl( domainResultCreationState ); final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl( domainResultCreationState );
final TableGroup parentTableGroup = creationStateImpl final TableGroup parentTableGroup = creationStateImpl
@ -137,4 +162,11 @@ public class ImplicitFetchBuilderBasic implements ImplicitFetchBuilder, BasicVal
result = 31 * result + fetchable.hashCode(); result = 31 * result + fetchable.hashCode();
return result; return result;
} }
@Override
public void visitFetchBuilders(BiConsumer<String, FetchBuilder> consumer) {
if ( fetchBuilder != null ) {
consumer.accept( fetchPath.getLocalName(), fetchBuilder );
}
}
} }