HHH-17193 Introduce `Fetch#asFetchParent()` to avoid instanceof checks

This commit is contained in:
Marco Belladelli 2023-10-05 10:49:58 +02:00
parent 8b4ef7c66c
commit cc99b8a67c
8 changed files with 36 additions and 4 deletions

View File

@ -536,6 +536,11 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
getKeyValueFetch().createAssembler( parentAccess, creationState )
);
}
@Override
public FetchParent asFetchParent() {
return this;
}
}
private static class AnyResultAssembler<T> implements DomainResultAssembler<T> {

View File

@ -37,6 +37,14 @@ public interface Fetch extends DomainResultGraphNode {
*/
FetchParent getFetchParent();
/**
* Utility method to avoid {@code instanceof} checks. Returns this if it's
* an instance of {@link FetchParent}, null otherwise.
*/
default FetchParent asFetchParent() {
return null;
}
/**
* The value mapping being fetched
*/

View File

@ -72,7 +72,7 @@ public interface FetchList extends Iterable<Fetch> {
if ( fetch instanceof EagerCollectionFetch ) {
return true;
}
else if ( fetch instanceof FetchParent && ( (FetchParent) fetch ).containsCollectionFetches() ) {
else if ( fetch.asFetchParent() != null && fetch.asFetchParent().containsCollectionFetches() ) {
return true;
}
}

View File

@ -220,4 +220,9 @@ public class EagerCollectionFetch extends CollectionFetch implements FetchParent
public JavaType<?> getResultJavaType() {
return getFetchedMapping().getJavaType();
}
@Override
public FetchParent asFetchParent() {
return this;
}
}

View File

@ -170,4 +170,9 @@ public class AggregateEmbeddableFetchImpl extends AbstractFetchParent implements
return new EmbeddableAssembler( initializer );
}
@Override
public FetchParent asFetchParent() {
return this;
}
}

View File

@ -168,4 +168,8 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab
return getFetchParent().appliesTo( graphImplementor, metamodel );
}
@Override
public FetchParent asFetchParent() {
return this;
}
}

View File

@ -43,13 +43,13 @@ public abstract class AbstractEntityResultGraphNode extends AbstractFetchParent
final TableGroup entityTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess()
.getTableGroup( navigablePath );
final EntityResultGraphNode entityResultGraphNode = (EntityResultGraphNode) fetchParent;
final Fetch fetch = creationState.visitIdentifierFetch( entityResultGraphNode );
final Fetch idFetch = creationState.visitIdentifierFetch( entityResultGraphNode );
if ( navigablePath.getParent() == null && !creationState.forceIdentifierSelection() &&
( !( fetch instanceof FetchParent ) || !( (FetchParent) fetch ).containsCollectionFetches() ) ) {
( idFetch.asFetchParent() == null || !idFetch.asFetchParent().containsCollectionFetches() ) ) {
identifierFetch = null;
}
else {
identifierFetch = fetch;
identifierFetch = idFetch;
}
discriminatorFetch = creationState.visitDiscriminatorFetch( entityResultGraphNode );

View File

@ -7,6 +7,7 @@
package org.hibernate.sql.results.graph.entity;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
/**
* Specialization of Fetch for entity-valued fetches
@ -19,4 +20,8 @@ public interface EntityFetch extends EntityResultGraphNode, Fetch {
return true;
}
@Override
default FetchParent asFetchParent() {
return this;
}
}