HHH-15674 @NamedEntityGraph not working on entity that have composite PK

This commit is contained in:
Andrea Boriero 2022-12-06 18:39:20 +01:00 committed by Andrea Boriero
parent ff28d677ab
commit 23cff8bd10
4 changed files with 23 additions and 14 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.sql.results.graph; package org.hibernate.sql.results.graph;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.spi.NavigablePath; import org.hibernate.spi.NavigablePath;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
@ -39,4 +40,9 @@ public interface DomainResultGraphNode {
// override this already to return it // override this already to return it
return null; return null;
} }
default boolean appliesTo(GraphImplementor graphImplementor){
return false;
}
} }

View File

@ -7,6 +7,7 @@
package org.hibernate.sql.results.graph.embeddable.internal; package org.hibernate.sql.results.graph.embeddable.internal;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.spi.NavigablePath; import org.hibernate.spi.NavigablePath;
@ -30,7 +31,6 @@ import org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class EmbeddableFetchImpl extends AbstractFetchParent implements EmbeddableResultGraphNode, Fetch { public class EmbeddableFetchImpl extends AbstractFetchParent implements EmbeddableResultGraphNode, Fetch {
private final EmbeddableValuedFetchable embeddedPartDescriptor;
private final FetchParent fetchParent; private final FetchParent fetchParent;
private final FetchTiming fetchTiming; private final FetchTiming fetchTiming;
@ -45,7 +45,6 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab
boolean hasTableGroup, boolean hasTableGroup,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
super( embeddedPartDescriptor.getEmbeddableTypeDescriptor(), navigablePath ); super( embeddedPartDescriptor.getEmbeddableTypeDescriptor(), navigablePath );
this.embeddedPartDescriptor = embeddedPartDescriptor;
this.fetchParent = fetchParent; this.fetchParent = fetchParent;
this.fetchTiming = fetchTiming; this.fetchTiming = fetchTiming;
@ -142,4 +141,10 @@ public class EmbeddableFetchImpl extends AbstractFetchParent implements Embeddab
return new EmbeddableAssembler( initializer ); return new EmbeddableAssembler( initializer );
} }
@Override
public boolean appliesTo(GraphImplementor graphImplementor) {
return getFetchParent().appliesTo( graphImplementor );
}
} }

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.sql.results.graph.entity; package org.hibernate.sql.results.graph.entity;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.sql.results.graph.DomainResultGraphNode; import org.hibernate.sql.results.graph.DomainResultGraphNode;
import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParent;
@ -38,4 +39,11 @@ public interface EntityResultGraphNode extends DomainResultGraphNode, FetchParen
default EntityMappingType getReferencedMappingContainer() { default EntityMappingType getReferencedMappingContainer() {
return getEntityValuedModelPart().getEntityMappingType(); return getEntityValuedModelPart().getEntityMappingType();
} }
@Override
default boolean appliesTo(GraphImplementor graphImplementor) {
return graphImplementor.appliesTo(
getEntityValuedModelPart().getEntityMappingType().getJavaType().getJavaTypeClass()
);
}
} }

View File

@ -17,14 +17,11 @@ import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.graph.spi.SubGraphImplementor; import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.internal.EntityCollectionPart; import org.hibernate.metamodel.mapping.internal.EntityCollectionPart;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.sql.results.graph.EntityGraphTraversalState; import org.hibernate.sql.results.graph.EntityGraphTraversalState;
import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.graph.entity.EntityResultGraphNode;
import jakarta.persistence.metamodel.PluralAttribute; import jakarta.persistence.metamodel.PluralAttribute;
@ -117,17 +114,10 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers
} }
private boolean appliesTo(FetchParent fetchParent) { private boolean appliesTo(FetchParent fetchParent) {
if ( currentGraphContext == null || !( fetchParent instanceof EntityResultGraphNode ) ) { if ( currentGraphContext == null ) {
return false; return false;
} }
return fetchParent.appliesTo( currentGraphContext );
final EntityResultGraphNode entityFetchParent = (EntityResultGraphNode) fetchParent;
final EntityMappingType entityFetchParentMappingType = entityFetchParent.getEntityValuedModelPart().getEntityMappingType();
assert currentGraphContext.getGraphedType() instanceof EntityDomainType;
final EntityDomainType entityDomainType = (EntityDomainType) currentGraphContext.getGraphedType();
return entityDomainType.getHibernateEntityName().equals( entityFetchParentMappingType.getEntityName() );
} }
private static boolean isJpaMapCollectionType(PluralAttributeMapping pluralAttributeMapping) { private static boolean isJpaMapCollectionType(PluralAttributeMapping pluralAttributeMapping) {