Search in sources :

Example 6 with StateArrayContributorMetadataAccess

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

the class BasicAttributeMapping method withSelectableMapping.

public static BasicAttributeMapping withSelectableMapping(ManagedMappingType declaringType, BasicValuedModelPart original, PropertyAccess propertyAccess, ValueGeneration valueGeneration, SelectableMapping selectableMapping) {
    String attributeName = null;
    int stateArrayPosition = 0;
    StateArrayContributorMetadataAccess attributeMetadataAccess = null;
    BasicValueConverter<?, ?> valueConverter = null;
    if (original instanceof SingleAttributeIdentifierMapping) {
        final SingleAttributeIdentifierMapping mapping = (SingleAttributeIdentifierMapping) original;
        attributeName = mapping.getAttributeName();
        attributeMetadataAccess = null;
    } else if (original instanceof SingularAttributeMapping) {
        final SingularAttributeMapping mapping = (SingularAttributeMapping) original;
        attributeName = mapping.getAttributeName();
        stateArrayPosition = mapping.getStateArrayPosition();
        attributeMetadataAccess = mapping.getAttributeMetadataAccess();
    }
    if (original instanceof ConvertibleModelPart) {
        valueConverter = ((ConvertibleModelPart) original).getValueConverter();
    }
    return new BasicAttributeMapping(attributeName, original.getNavigableRole(), stateArrayPosition, attributeMetadataAccess, FetchTiming.IMMEDIATE, FetchStyle.JOIN, selectableMapping.getContainingTableExpression(), selectableMapping.getSelectionExpression(), selectableMapping.isFormula(), selectableMapping.getCustomReadExpression(), selectableMapping.getCustomWriteExpression(), selectableMapping.getColumnDefinition(), selectableMapping.getLength(), selectableMapping.getPrecision(), selectableMapping.getScale(), valueConverter, original.getJdbcMapping(), declaringType, propertyAccess, valueGeneration);
}
Also used : StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) ConvertibleModelPart(org.hibernate.metamodel.mapping.ConvertibleModelPart) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 7 with StateArrayContributorMetadataAccess

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

the class AbstractEmbeddableMapping method finishInitialization.

protected static boolean finishInitialization(NavigableRole navigableRole, Component bootDescriptor, CompositeType compositeType, String rootTableExpression, String[] rootTableKeyColumnNames, EmbeddableMappingType declarer, EmbeddableRepresentationStrategy representationStrategy, AttributeTypeValidator attributeTypeValidator, ConcreteTableResolver concreteTableResolver, Consumer<AttributeMapping> attributeConsumer, SuccessfulCompletionCallback completionCallback, MappingModelCreationProcess creationProcess) {
    final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
    final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final Dialect dialect = jdbcEnvironment.getDialect();
    final Type[] subtypes = compositeType.getSubtypes();
    int attributeIndex = 0;
    int columnPosition = 0;
    for (Property bootPropertyDescriptor : bootDescriptor.getProperties()) {
        final Type subtype = subtypes[attributeIndex];
        attributeTypeValidator.check(bootPropertyDescriptor.getName(), subtype);
        final PropertyAccess propertyAccess = representationStrategy.resolvePropertyAccess(bootPropertyDescriptor);
        final AttributeMapping attributeMapping;
        if (subtype instanceof BasicType) {
            final BasicValue basicValue = (BasicValue) bootPropertyDescriptor.getValue();
            final Selectable selectable = basicValue.getColumn();
            final String containingTableExpression;
            final String columnExpression;
            if (rootTableKeyColumnNames == null) {
                if (selectable.isFormula()) {
                    columnExpression = selectable.getTemplate(dialect, creationProcess.getSqmFunctionRegistry());
                } else {
                    columnExpression = selectable.getText(dialect);
                }
                if (selectable instanceof Column) {
                    containingTableExpression = concreteTableResolver.resolve((Column) selectable, jdbcEnvironment);
                } else {
                    containingTableExpression = rootTableExpression;
                }
            } else {
                containingTableExpression = rootTableExpression;
                columnExpression = rootTableKeyColumnNames[columnPosition];
            }
            final String columnDefinition;
            final Long length;
            final Integer precision;
            final Integer scale;
            if (selectable instanceof Column) {
                Column column = (Column) selectable;
                columnDefinition = column.getSqlType();
                length = column.getLength();
                precision = column.getPrecision();
                scale = column.getScale();
            } else {
                columnDefinition = null;
                length = null;
                precision = null;
                scale = null;
            }
            attributeMapping = MappingModelCreationHelper.buildBasicAttributeMapping(bootPropertyDescriptor.getName(), navigableRole.append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, declarer, (BasicType<?>) subtype, containingTableExpression, columnExpression, selectable.isFormula(), selectable.getCustomReadExpression(), selectable.getCustomWriteExpression(), columnDefinition, length, precision, scale, propertyAccess, compositeType.getCascadeStyle(attributeIndex), creationProcess);
            columnPosition++;
        } else if (subtype instanceof AnyType) {
            final Any bootValueMapping = (Any) bootPropertyDescriptor.getValue();
            final AnyType anyType = (AnyType) subtype;
            final boolean nullable = bootValueMapping.isNullable();
            final boolean insertable = bootPropertyDescriptor.isInsertable();
            final boolean updateable = bootPropertyDescriptor.isUpdateable();
            final boolean includeInOptimisticLocking = bootPropertyDescriptor.isOptimisticLocked();
            final CascadeStyle cascadeStyle = compositeType.getCascadeStyle(attributeIndex);
            final MutabilityPlan<?> mutabilityPlan;
            if (updateable) {
                mutabilityPlan = new MutabilityPlan<>() {

                    @Override
                    public boolean isMutable() {
                        return true;
                    }

                    @Override
                    public Object deepCopy(Object value) {
                        if (value == null) {
                            return null;
                        }
                        return anyType.deepCopy(value, creationProcess.getCreationContext().getSessionFactory());
                    }

                    @Override
                    public Serializable disassemble(Object value, SharedSessionContract session) {
                        throw new NotYetImplementedFor6Exception(getClass());
                    }

                    @Override
                    public Object assemble(Serializable cached, SharedSessionContract session) {
                        throw new NotYetImplementedFor6Exception(getClass());
                    }
                };
            } else {
                mutabilityPlan = ImmutableMutabilityPlan.INSTANCE;
            }
            final StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {

                @Override
                public PropertyAccess getPropertyAccess() {
                    return propertyAccess;
                }

                @Override
                public MutabilityPlan<?> getMutabilityPlan() {
                    return mutabilityPlan;
                }

                @Override
                public boolean isNullable() {
                    return nullable;
                }

                @Override
                public boolean isInsertable() {
                    return insertable;
                }

                @Override
                public boolean isUpdatable() {
                    return updateable;
                }

                @Override
                public boolean isIncludedInDirtyChecking() {
                    // todo (6.0) : do not believe this is correct
                    return updateable;
                }

                @Override
                public boolean isIncludedInOptimisticLocking() {
                    return includeInOptimisticLocking;
                }

                @Override
                public CascadeStyle getCascadeStyle() {
                    return cascadeStyle;
                }
            };
            attributeMapping = new DiscriminatedAssociationAttributeMapping(navigableRole.append(bootPropertyDescriptor.getName()), typeConfiguration.getJavaTypeRegistry().getDescriptor(Object.class), declarer, attributeIndex, attributeMetadataAccess, bootPropertyDescriptor.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE, propertyAccess, bootPropertyDescriptor, anyType, bootValueMapping, creationProcess);
        } else if (subtype instanceof CompositeType) {
            final CompositeType subCompositeType = (CompositeType) subtype;
            final int columnSpan = subCompositeType.getColumnSpan(sessionFactory);
            final String subTableExpression;
            final String[] subRootTableKeyColumnNames;
            if (rootTableKeyColumnNames == null) {
                subTableExpression = rootTableExpression;
                subRootTableKeyColumnNames = null;
            } else {
                subTableExpression = rootTableExpression;
                subRootTableKeyColumnNames = new String[columnSpan];
                System.arraycopy(rootTableKeyColumnNames, columnPosition, subRootTableKeyColumnNames, 0, columnSpan);
            }
            attributeMapping = MappingModelCreationHelper.buildEmbeddedAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, declarer, subCompositeType, subTableExpression, subRootTableKeyColumnNames, propertyAccess, compositeType.getCascadeStyle(attributeIndex), creationProcess);
            columnPosition += columnSpan;
        } else if (subtype instanceof CollectionType) {
            final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
            attributeMapping = MappingModelCreationHelper.buildPluralAttributeMapping(bootPropertyDescriptor.getName(), attributeIndex, bootPropertyDescriptor, entityPersister, propertyAccess, compositeType.getCascadeStyle(attributeIndex), compositeType.getFetchMode(attributeIndex), creationProcess);
        } else if (subtype instanceof EntityType) {
            final EntityPersister entityPersister = creationProcess.getEntityPersister(bootDescriptor.getOwner().getEntityName());
            attributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(bootPropertyDescriptor.getName(), navigableRole.append(bootPropertyDescriptor.getName()), attributeIndex, bootPropertyDescriptor, entityPersister, entityPersister, (EntityType) subtype, representationStrategy.resolvePropertyAccess(bootPropertyDescriptor), compositeType.getCascadeStyle(attributeIndex), creationProcess);
            columnPosition += bootPropertyDescriptor.getColumnSpan();
        } else {
            throw new MappingException(String.format(Locale.ROOT, "Unable to determine attribute nature : %s#%s", bootDescriptor.getOwner().getEntityName(), bootPropertyDescriptor.getName()));
        }
        attributeConsumer.accept(attributeMapping);
        attributeIndex++;
    }
    completionCallback.success();
    return true;
}
Also used : CascadeStyle(org.hibernate.engine.spi.CascadeStyle) EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) BasicType(org.hibernate.type.BasicType) SharedSessionContract(org.hibernate.SharedSessionContract) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Any(org.hibernate.mapping.Any) BasicValue(org.hibernate.mapping.BasicValue) MappingException(org.hibernate.MappingException) Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) CollectionType(org.hibernate.type.CollectionType) Dialect(org.hibernate.dialect.Dialect) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) Property(org.hibernate.mapping.Property) AnyType(org.hibernate.type.AnyType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityType(org.hibernate.type.EntityType) BasicType(org.hibernate.type.BasicType) AnyType(org.hibernate.type.AnyType) JavaType(org.hibernate.type.descriptor.java.JavaType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) Type(org.hibernate.type.Type) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) CompositeType(org.hibernate.type.CompositeType)

Aggregations

StateArrayContributorMetadataAccess (org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess)6 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)4 Dialect (org.hibernate.dialect.Dialect)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Serializable (java.io.Serializable)3 MappingException (org.hibernate.MappingException)3 SharedSessionContract (org.hibernate.SharedSessionContract)3 CascadeStyle (org.hibernate.engine.spi.CascadeStyle)3 Any (org.hibernate.mapping.Any)3 BasicValue (org.hibernate.mapping.BasicValue)3 Property (org.hibernate.mapping.Property)3 Selectable (org.hibernate.mapping.Selectable)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 FetchStyle (org.hibernate.engine.FetchStyle)2 FetchTiming (org.hibernate.engine.FetchTiming)2 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)2 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 Column (org.hibernate.mapping.Column)2 Component (org.hibernate.mapping.Component)2 ManyToOne (org.hibernate.mapping.ManyToOne)2