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
}
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));
}
Aggregations