Search in sources :

Example 26 with ModelPart

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

the class GeometryMappingTest method testSimpleEntity.

@Test
public void testSimpleEntity(SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(MLEntity.class);
    final JdbcTypeRegistry jdbcTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJdbcTypeRegistry();
    final JavaTypeRegistry javaTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJavaTypeRegistry();
    ModelPart part = entityDescriptor.findSubPart("lineString");
    assertThat(part.getJavaType(), equalTo(GeolatteGeometryJavaType.MULTILINESTRING_INSTANCE));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) ModelPart(org.hibernate.metamodel.mapping.ModelPart) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 27 with ModelPart

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

the class AbstractEntityPersister method findSubPart.

@Override
public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
    LOG.tracef("#findSubPart(`%s`)", name);
    if (EntityDiscriminatorMapping.matchesRoleName(name)) {
        return discriminatorMapping;
    }
    final AttributeMapping declaredAttribute = declaredAttributeMappings.get(name);
    if (declaredAttribute != null) {
        return declaredAttribute;
    }
    if (superMappingType != null) {
        final ModelPart superDefinedAttribute = superMappingType.findSubPart(name, superMappingType);
        if (superDefinedAttribute != null) {
            // Prefer the identifier mapping of the concrete class
            if (superDefinedAttribute instanceof EntityIdentifierMapping) {
                final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
                if (identifierModelPart != null) {
                    return identifierModelPart;
                }
            }
            return superDefinedAttribute;
        }
    }
    if (treatTargetType != null) {
        if (!treatTargetType.isTypeOrSuperType(this)) {
            return null;
        }
        if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
            for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
                if (!treatTargetType.isTypeOrSuperType(subMappingType)) {
                    continue;
                }
                final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
                if (subDefinedAttribute != null) {
                    return subDefinedAttribute;
                }
            }
        }
    } else {
        if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
            ModelPart attribute = null;
            for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
                final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
                if (subDefinedAttribute != null) {
                    if (attribute != null && !MappingModelHelper.isCompatibleModelPart(attribute, subDefinedAttribute)) {
                        throw new IllegalArgumentException(new SemanticException(String.format(Locale.ROOT, "Could not resolve attribute '%s' of '%s' due to the attribute being declared in multiple sub types: ['%s', '%s']", name, getJavaType().getJavaType().getTypeName(), ((AttributeMapping) attribute).getDeclaringType().getJavaType().getJavaType().getTypeName(), ((AttributeMapping) subDefinedAttribute).getDeclaringType().getJavaType().getJavaType().getTypeName())));
                    }
                    attribute = subDefinedAttribute;
                }
            }
            if (attribute != null) {
                return attribute;
            }
        }
    }
    final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
    if (identifierModelPart != null) {
        return identifierModelPart;
    }
    for (AttributeMapping attribute : declaredAttributeMappings.values()) {
        if (attribute instanceof EmbeddableValuedModelPart && attribute instanceof VirtualModelPart) {
            final ModelPart subPart = ((EmbeddableValuedModelPart) attribute).findSubPart(name, null);
            if (subPart != null) {
                return subPart;
            }
        }
    }
    return null;
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) DiscriminatedAssociationModelPart(org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) DiscriminatedAssociationAttributeMapping(org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) InFlightEntityMappingType(org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType) SemanticException(org.hibernate.query.SemanticException) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart)

Example 28 with ModelPart

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

the class CompleteResultBuilderCollectionStandard method resolveSelections.

private void resolveSelections(TableGroup tableGroup, ModelPart modelPart, String[] columnNames, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationStateImpl creationStateImpl) {
    final SelectableConsumer consumer = (selectionIndex, selectableMapping) -> {
        final String columnName = columnNames[selectionIndex];
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
            final BasicValuedMapping basicType = (BasicValuedMapping) selectableMapping.getJdbcMapping();
            final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
        }), selectableMapping.getJdbcMapping().getMappedJavaType(), null, creationStateImpl.getSessionFactory().getTypeConfiguration());
    };
    if (modelPart instanceof EntityValuedModelPart) {
        final EntityMappingType entityMappingType = ((EntityValuedModelPart) modelPart).getEntityMappingType();
        int index = entityMappingType.getIdentifierMapping().forEachSelectable(consumer);
        if (entityMappingType.getDiscriminatorMapping() != null) {
            index += entityMappingType.getDiscriminatorMapping().forEachSelectable(index, consumer);
        }
        entityMappingType.forEachSelectable(index, consumer);
    } else {
        modelPart.forEachSelectable(consumer);
    }
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) Arrays(java.util.Arrays) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) FromClauseAccessImpl(org.hibernate.query.results.FromClauseAccessImpl) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) NavigablePath(org.hibernate.spi.NavigablePath) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) ModelPart(org.hibernate.metamodel.mapping.ModelPart) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) NativeQuery(org.hibernate.query.NativeQuery) ResultBuilder(org.hibernate.query.results.ResultBuilder) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 29 with ModelPart

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

the class ResultsHelper method createRowReader.

public static <R> RowReader<R> createRowReader(ExecutionContext executionContext, LockOptions lockOptions, RowTransformer<R> rowTransformer, Class<R> transformedResultJavaType, JdbcValues jdbcValues) {
    final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory();
    final Map<NavigablePath, Initializer> initializerMap = new LinkedHashMap<>();
    final List<Initializer> initializers = new ArrayList<>();
    final List<DomainResultAssembler<?>> assemblers = jdbcValues.getValuesMapping().resolveAssemblers(new AssemblerCreationState() {

        @Override
        public LockMode determineEffectiveLockMode(String identificationVariable) {
            return lockOptions.getEffectiveLockMode(identificationVariable);
        }

        @Override
        public Initializer resolveInitializer(NavigablePath navigablePath, ModelPart fetchedModelPart, Supplier<Initializer> producer) {
            final Initializer existing = initializerMap.get(navigablePath);
            if (existing != null) {
                if (fetchedModelPart.getNavigableRole().equals(existing.getInitializedPart().getNavigableRole())) {
                    ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef("Returning previously-registered initializer : %s", existing);
                    return existing;
                }
            }
            final Initializer initializer = producer.get();
            ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef("Registering initializer : %s", initializer);
            initializerMap.put(navigablePath, initializer);
            initializers.add(initializer);
            return initializer;
        }

        @Override
        public SqlAstCreationContext getSqlAstCreationContext() {
            return sessionFactory;
        }
    });
    logInitializers(initializerMap);
    return new StandardRowReader<>(assemblers, initializers, rowTransformer, transformedResultJavaType);
}
Also used : NavigablePath(org.hibernate.spi.NavigablePath) ModelPart(org.hibernate.metamodel.mapping.ModelPart) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ArrayList(java.util.ArrayList) LockMode(org.hibernate.LockMode) AssemblerCreationState(org.hibernate.sql.results.graph.AssemblerCreationState) LinkedHashMap(java.util.LinkedHashMap) DomainResultAssembler(org.hibernate.sql.results.graph.DomainResultAssembler) Initializer(org.hibernate.sql.results.graph.Initializer) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext)

Example 30 with ModelPart

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

the class InverseNonAggregatedIdentifierMapping method getIdentifier.

@Override
public Object getIdentifier(Object entity) {
    if (hasContainingClass()) {
        final Object id = identifierValueMapper.getRepresentationStrategy().getInstantiator().instantiate(null, // sessionFactory
        null);
        final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
        final List<AttributeMapping> idClassAttributeMappings = identifierValueMapper.getAttributeMappings();
        final Object[] propertyValues = new Object[attributeMappings.size()];
        for (int i = 0; i < propertyValues.length; i++) {
            final AttributeMapping attributeMapping = attributeMappings.get(i);
            final Object o = attributeMapping.getPropertyAccess().getGetter().get(entity);
            if (o == null) {
                final AttributeMapping idClassAttributeMapping = idClassAttributeMappings.get(i);
                if (idClassAttributeMapping.getPropertyAccess().getGetter().getReturnTypeClass().isPrimitive()) {
                    propertyValues[i] = idClassAttributeMapping.getExpressibleJavaType().getDefaultValue();
                } else {
                    propertyValues[i] = null;
                }
            } else // JPA 2 @MapsId + @IdClass points to the pk of the entity
            if (attributeMapping instanceof ToOneAttributeMapping && !(idClassAttributeMappings.get(i) instanceof ToOneAttributeMapping)) {
                final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
                final ModelPart targetPart = toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature().inverse());
                if (targetPart instanceof EntityIdentifierMapping) {
                    propertyValues[i] = ((EntityIdentifierMapping) targetPart).getIdentifier(o);
                } else {
                    propertyValues[i] = o;
                    assert false;
                }
            } else {
                propertyValues[i] = o;
            }
        }
        identifierValueMapper.setValues(id, propertyValues);
        return id;
    } else {
        return entity;
    }
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping)

Aggregations

ModelPart (org.hibernate.metamodel.mapping.ModelPart)45 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)21 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)21 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)16 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)15 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)14 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)13 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)13 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)13 NavigablePath (org.hibernate.spi.NavigablePath)13 ArrayList (java.util.ArrayList)12 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)11 Test (org.junit.jupiter.api.Test)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)9 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)9 Expression (org.hibernate.sql.ast.tree.expression.Expression)9 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)9 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)8 ConvertibleModelPart (org.hibernate.metamodel.mapping.ConvertibleModelPart)8