Search in sources :

Example 1 with SimpleForeignKeyDescriptor

use of org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor in project hibernate-orm by hibernate.

the class NonRootTablePolymorphicTests method verifyRuntimeModel.

@Test
public void verifyRuntimeModel(SessionFactoryScope scope) {
    final EntityMappingType rootEntity = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Root.class);
    final EntityMappingType subEntity = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Sub.class);
    final EntityMappingType leafEntity = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Leaf.class);
    final EntityMappingType childEntity = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(SubChild.class);
    final EntityMappingType parentEntity = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(SubParent.class);
    // check Sub#child fk
    final ToOneAttributeMapping childAttribute = (ToOneAttributeMapping) subEntity.findAttributeMapping("child");
    final SimpleForeignKeyDescriptor childFk = (SimpleForeignKeyDescriptor) childAttribute.getForeignKeyDescriptor();
    assertThat(childFk.getKeyTable(), is("sub"));
    assertThat(childFk.getTargetTable(), is("sub_child"));
    assertThat(childFk.getJdbcTypeCount(), is(1));
    assertThat(childFk.getKeyPart().getSelectionExpression(), is("child_fk"));
    assertThat(childFk.getTargetPart().getSelectionExpression(), is("child_id"));
    // check Parent#sub fk
    final ToOneAttributeMapping subAttribute = (ToOneAttributeMapping) parentEntity.findAttributeMapping("sub");
    final SimpleForeignKeyDescriptor subFk = (SimpleForeignKeyDescriptor) subAttribute.getForeignKeyDescriptor();
    assertThat(subFk.getKeyTable(), is("sub_parent"));
    assertThat(subFk.getTargetTable(), is("sub"));
    assertThat(subFk.getJdbcTypeCount(), is(1));
    assertThat(subFk.getKeyPart().getSelectionExpression(), is("parent_sub_fk"));
    assertThat(subFk.getTargetPart().getSelectionExpression(), is("sub_id"));
    scope.inTransaction((session) -> {
        session.createQuery("from SubParent p join fetch p.sub").list();
        session.createQuery("from SubGroup p join fetch p.manyToManySubs").list();
        session.createQuery("from SubGroup p join fetch p.oneToManySubs").list();
    });
// for sure the inheritance keys are messed up in the mapping model
}
Also used : ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleForeignKeyDescriptor

use of org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor in project hibernate-orm by hibernate.

the class LoaderSelectBuilder method applySubSelectRestriction.

private void applySubSelectRestriction(QuerySpec querySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, SubselectFetch subselect, LoaderSqlAstCreationState sqlAstCreationState) {
    final SqlAstCreationContext sqlAstCreationContext = sqlAstCreationState.getCreationContext();
    final SessionFactoryImplementor sessionFactory = sqlAstCreationContext.getSessionFactory();
    assert loadable instanceof PluralAttributeMapping;
    final PluralAttributeMapping attributeMapping = (PluralAttributeMapping) loadable;
    final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
    final NavigablePath navigablePath = rootNavigablePath.append(attributeMapping.getAttributeName());
    final Expression fkExpression;
    final int jdbcTypeCount = fkDescriptor.getJdbcTypeCount();
    if (jdbcTypeCount == 1) {
        assert fkDescriptor instanceof SimpleForeignKeyDescriptor;
        final SimpleForeignKeyDescriptor simpleFkDescriptor = (SimpleForeignKeyDescriptor) fkDescriptor;
        final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, simpleFkDescriptor.getContainingTableExpression());
        fkExpression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, simpleFkDescriptor.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, simpleFkDescriptor.getSelectionExpression(), false, null, null, simpleFkDescriptor.getJdbcMapping(), this.creationContext.getSessionFactory()));
    } else {
        final List<ColumnReference> columnReferences = new ArrayList<>(jdbcTypeCount);
        fkDescriptor.forEachSelectable((columnIndex, selection) -> {
            final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
            columnReferences.add((ColumnReference) sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, this.creationContext.getSessionFactory())));
        });
        fkExpression = new SqlTuple(columnReferences, fkDescriptor);
    }
    querySpec.applyPredicate(new InSubQueryPredicate(fkExpression, generateSubSelect(attributeMapping, rootTableGroup, subselect, jdbcTypeCount, sqlAstCreationState, sessionFactory), false));
}
Also used : Arrays(java.util.Arrays) CollectionFetch(org.hibernate.sql.results.graph.collection.internal.CollectionFetch) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) GraphSemantic(org.hibernate.graph.GraphSemantic) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultImpl) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) BagSemantics(org.hibernate.collection.spi.BagSemantics) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) FetchProfile(org.hibernate.engine.profile.FetchProfile) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Fetchable(org.hibernate.sql.results.graph.Fetchable) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LockOptions(org.hibernate.LockOptions) NavigablePath(org.hibernate.query.spi.NavigablePath) AliasCollector(org.hibernate.sql.ast.spi.AliasCollector) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderByFragment(org.hibernate.metamodel.mapping.ordering.OrderByFragment) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Logger(org.jboss.logging.Logger) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) ArrayList(java.util.ArrayList) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) Loader(org.hibernate.loader.ast.spi.Loader) BiConsumer(java.util.function.BiConsumer) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Loadable(org.hibernate.loader.ast.spi.Loadable) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) Restrictable(org.hibernate.metamodel.mapping.Restrictable) EntityGraphTraversalState(org.hibernate.sql.results.graph.EntityGraphTraversalState) FetchStyle(org.hibernate.engine.FetchStyle) StandardEntityGraphTraversalStateImpl(org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl) AbstractMap(java.util.AbstractMap) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ArrayList(java.util.ArrayList) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) Expression(org.hibernate.sql.ast.tree.expression.Expression) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

SimpleForeignKeyDescriptor (org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 LockOptions (org.hibernate.LockOptions)1 BagSemantics (org.hibernate.collection.spi.BagSemantics)1 FetchStyle (org.hibernate.engine.FetchStyle)1 FetchTiming (org.hibernate.engine.FetchTiming)1 FetchProfile (org.hibernate.engine.profile.FetchProfile)1 CascadeStyle (org.hibernate.engine.spi.CascadeStyle)1 CascadingAction (org.hibernate.engine.spi.CascadingAction)1 EffectiveEntityGraph (org.hibernate.engine.spi.EffectiveEntityGraph)1 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1