use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class EntityValuedPathInterpretation method from.
public static <T> EntityValuedPathInterpretation<T> from(SqmEntityValuedSimplePath<T> sqmPath, MappingModelExpressible<?> inferredMapping, SqmToSqlAstConverter sqlAstCreationState) {
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath());
final EntityValuedModelPart pathMapping = (EntityValuedModelPart) sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath()).getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
final EntityValuedModelPart mapping;
if (inferredMapping instanceof EntityAssociationMapping) {
final EntityAssociationMapping inferredAssociation = (EntityAssociationMapping) inferredMapping;
if (pathMapping instanceof EntityAssociationMapping && inferredMapping != pathMapping) {
// In here, the inferred mapping and the actual path mapping are association mappings,
// but for different associations, so we have to check if both associations point to the same target
final EntityAssociationMapping pathAssociation = (EntityAssociationMapping) pathMapping;
final ModelPart pathTargetPart = pathAssociation.getForeignKeyDescriptor().getPart(pathAssociation.getSideNature().inverse());
final ModelPart inferredTargetPart = inferredAssociation.getForeignKeyDescriptor().getPart(inferredAssociation.getSideNature().inverse());
// which will render the FK of the path association
if (pathTargetPart == inferredTargetPart) {
mapping = pathMapping;
} else {
// Otherwise, we need to use the entity mapping type to force rendering the PK
// for e.g. `a.assoc1 = a.assoc2` when both associations have different target join columns
mapping = pathMapping.getEntityMappingType();
}
} else {
// This is the case when the inferred mapping is an association, but the path mapping is not,
// or the path mapping and the inferred mapping are for the same association
mapping = (EntityValuedModelPart) inferredMapping;
}
} else {
mapping = pathMapping;
}
final ModelPart resultModelPart;
if (mapping instanceof EntityAssociationMapping) {
final EntityAssociationMapping associationMapping = (EntityAssociationMapping) mapping;
final ModelPart keyTargetMatchPart = associationMapping.getKeyTargetMatchPart();
if (keyTargetMatchPart instanceof ToOneAttributeMapping) {
resultModelPart = ((ToOneAttributeMapping) keyTargetMatchPart).getKeyTargetMatchPart();
} else {
resultModelPart = keyTargetMatchPart;
}
} else {
resultModelPart = mapping.getEntityMappingType().getIdentifierMapping();
}
return from(sqmPath.getNavigablePath(), tableGroup, resultModelPart, mapping, mapping, sqlAstCreationState);
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class BasicValuedPathInterpretation method from.
/**
* Static factory
*/
public static <T> BasicValuedPathInterpretation<T> from(SqmBasicValuedSimplePath<T> sqmPath, SqlAstCreationState sqlAstCreationState, SemanticQueryWalker sqmWalker, boolean jpaQueryComplianceEnabled) {
final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
final TableGroup tableGroup = fromClauseAccess.getTableGroup(sqmPath.getNavigablePath().getParent());
EntityMappingType treatTarget = null;
if (jpaQueryComplianceEnabled) {
if (sqmPath.getLhs() instanceof SqmTreatedPath) {
final EntityDomainType treatTargetDomainType = ((SqmTreatedPath) sqmPath.getLhs()).getTreatTarget();
final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
treatTarget = mappingMetamodel.findEntityDescriptor(treatTargetDomainType.getHibernateEntityName());
} else if (sqmPath.getLhs().getNodeType() instanceof EntityDomainType) {
final EntityDomainType entityDomainType = (EntityDomainType) sqmPath.getLhs().getNodeType();
final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
treatTarget = mappingMetamodel.findEntityDescriptor(entityDomainType.getHibernateEntityName());
}
}
final BasicValuedModelPart mapping = (BasicValuedModelPart) tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), treatTarget);
if (mapping == null) {
if (jpaQueryComplianceEnabled) {
// to get the better error, see if we got nothing because of treat handling
final ModelPart subPart = tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
if (subPart != null) {
throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.IMPLICIT_TREAT);
}
}
throw new SemanticException("`" + sqmPath.getNavigablePath() + "` did not reference a known model part");
}
final TableReference tableReference = tableGroup.resolveTableReference(sqmPath.getNavigablePath(), mapping.getContainingTableExpression());
final Expression expression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mapping.getSelectionExpression()), sacs -> new ColumnReference(tableReference.getIdentificationVariable(), mapping, sqlAstCreationState.getCreationContext().getSessionFactory()));
final ColumnReference columnReference;
if (expression instanceof ColumnReference) {
columnReference = ((ColumnReference) expression);
} else if (expression instanceof SqlSelectionExpression) {
final Expression selectedExpression = ((SqlSelectionExpression) expression).getSelection().getExpression();
assert selectedExpression instanceof ColumnReference;
columnReference = (ColumnReference) selectedExpression;
} else {
throw new UnsupportedOperationException("Unsupported basic-valued path expression : " + expression);
}
return new BasicValuedPathInterpretation<>(columnReference, sqmPath.getNavigablePath(), mapping, tableGroup);
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class SmokeTests method testEntityBasedManyToOne.
@Test
public void testEntityBasedManyToOne(SessionFactoryScope scope) {
final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(OtherEntity.class);
final EntityPersister simpleEntityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(SimpleEntity.class);
final ModelPart part = entityDescriptor.findSubPart("simpleEntity");
assertThat(part, notNullValue());
assertThat(part, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping attrMapping = (ToOneAttributeMapping) part;
assertThat(attrMapping.getAttributeName(), is("simpleEntity"));
assertThat(attrMapping.getMappedType(), is(simpleEntityDescriptor));
assertThat(attrMapping.getJavaType(), is(simpleEntityDescriptor.getJavaType()));
assertThat(attrMapping.getDeclaringType(), is(entityDescriptor));
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class ManyToOneTest method basicTest.
@Test
public void basicTest(SessionFactoryScope scope) {
final EntityPersister otherDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(OtherEntity.class);
final ModelPart simpleEntityAssociation = otherDescriptor.findSubPart("simpleEntity");
assertThat(simpleEntityAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("other_entity"));
assertThat(selection.getSelectionExpression(), is("simple_entity_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("simple_entity"));
assertThat(selection.getSelectionExpression(), is("id"));
});
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class EntityWithBidirectionalAssociationTest method basicTest.
@Test
public void basicTest(SessionFactoryScope scope) {
final EntityPersister parentDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Parent.class);
final ModelPart childAssociation = parentDescriptor.findSubPart("child");
assertThat(childAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT"));
assertThat(selection.getSelectionExpression(), is("child_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("CHILD"));
assertThat(selection.getSelectionExpression(), is("id"));
});
final EntityPersister childDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Child.class);
final ModelPart parentAssociation = childDescriptor.findSubPart("parent");
assertThat(parentAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT"));
assertThat(selection.getSelectionExpression(), is("child_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("CHILD"));
assertThat(selection.getSelectionExpression(), is("id"));
});
}
Aggregations