HHH-15423 @Any discriminated association fails to be implicitly fetched due to UnsupportedOperationException

This commit is contained in:
Andrea Boriero 2022-08-02 14:38:57 +02:00 committed by Andrea Boriero
parent 1c3549c9c5
commit 286b976fc5
3 changed files with 118 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import jakarta.persistence.metamodel.EntityType;
import jakarta.persistence.metamodel.SingularAttribute;
import org.hibernate.LockMode;
import org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping;
import org.hibernate.query.NativeQuery;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -26,6 +27,7 @@ import org.hibernate.metamodel.mapping.SingularAttributeMapping;
import org.hibernate.metamodel.mapping.internal.EntityCollectionPart;
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.results.implicit.ImplicitFetchBuilderDiscriminatedAssociation;
import org.hibernate.spi.NavigablePath;
import org.hibernate.query.internal.ResultSetMappingResolutionContext;
import org.hibernate.query.results.dynamic.DynamicResultBuilderAttribute;
@ -276,6 +278,15 @@ public class Builders {
return new ImplicitFetchBuilderEntityPart( fetchPath, (EntityCollectionPart) fetchable );
}
if ( fetchable instanceof DiscriminatedAssociationAttributeMapping ) {
return new ImplicitFetchBuilderDiscriminatedAssociation(
fetchPath,
(DiscriminatedAssociationAttributeMapping) fetchable,
creationState
);
}
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,107 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query.results.implicit;
import java.util.function.BiFunction;
import org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping;
import org.hibernate.query.results.DomainResultCreationStateImpl;
import org.hibernate.query.results.FetchBuilder;
import org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata;
import static org.hibernate.query.results.ResultsHelper.impl;
public class ImplicitFetchBuilderDiscriminatedAssociation implements ImplicitFetchBuilder {
private final NavigablePath fetchPath;
private final DiscriminatedAssociationAttributeMapping fetchable;
public ImplicitFetchBuilderDiscriminatedAssociation(
NavigablePath fetchPath,
DiscriminatedAssociationAttributeMapping fetchable,
DomainResultCreationState creationState) {
this.fetchPath = fetchPath;
this.fetchable = fetchable;
}
@Override
public FetchBuilder cacheKeyInstance() {
return this;
}
@Override
public Fetch buildFetch(
FetchParent parent,
NavigablePath fetchPath,
JdbcValuesMetadata jdbcResultsMetadata,
BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver,
DomainResultCreationState creationState) {
final DomainResultCreationStateImpl creationStateImpl = impl( creationState );
creationStateImpl.getFromClauseAccess().resolveTableGroup(
fetchPath,
navigablePath -> {
final TableGroup parentTableGroup = creationStateImpl
.getFromClauseAccess()
.getTableGroup( parent.getNavigablePath() );
final TableGroupJoin tableGroupJoin = fetchable.createTableGroupJoin(
fetchPath,
parentTableGroup,
null,
SqlAstJoinType.INNER,
true,
false,
creationStateImpl
);
parentTableGroup.addTableGroupJoin( tableGroupJoin );
return tableGroupJoin.getJoinedGroup();
}
);
return parent.generateFetchableFetch(
fetchable,
fetchPath,
fetchable.getTiming(),
false,
null,
creationState
);
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
final ImplicitFetchBuilderDiscriminatedAssociation that = (ImplicitFetchBuilderDiscriminatedAssociation) o;
return fetchPath.equals( that.fetchPath )
&& fetchable.equals( that.fetchable );
}
@Override
public int hashCode() {
int result = fetchPath.hashCode();
result = 31 * result + fetchable.hashCode();
return result;
}
@Override
public String toString() {
return "ImplicitFetchBuilderDiscriminatedAssociation(" + fetchPath + ")";
}
}

View File

@ -48,7 +48,6 @@ public class ImplicitFetchBuilderPlural implements ImplicitFetchBuilder {
JdbcValuesMetadata jdbcResultsMetadata,
BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver,
DomainResultCreationState creationState) {
final DomainResultCreationStateImpl creationStateImpl = impl( creationState );
final Fetch fetch = parent.generateFetchableFetch(
fetchable,