Search in sources :

Example 1 with ManagedMappingType

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

the class MappingModelCreationHelper method buildBasicAttributeMapping.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Non-identifier attributes
@SuppressWarnings("rawtypes")
public static BasicAttributeMapping buildBasicAttributeMapping(String attrName, NavigableRole navigableRole, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, BasicType attrType, String tableExpression, String attrColumnName, boolean isAttrFormula, String readExpr, String writeExpr, String columnDefinition, Long length, Integer precision, Integer scale, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, MappingModelCreationProcess creationProcess) {
    final Value value = bootProperty.getValue();
    final BasicValue.Resolution<?> resolution = ((Resolvable) value).resolve();
    final BasicValueConverter<?, ?> valueConverter = resolution.getValueConverter();
    final StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {

        private final MutabilityPlan mutabilityPlan = resolution.getMutabilityPlan();

        private final boolean nullable = value.isNullable();

        private final boolean insertable = bootProperty.isInsertable();

        private final boolean updateable = bootProperty.isUpdateable();

        private final boolean includeInOptimisticLocking = bootProperty.isOptimisticLocked();

        @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;
        }
    };
    final FetchTiming fetchTiming;
    final FetchStyle fetchStyle;
    if (declaringType instanceof EmbeddableMappingType) {
        if (bootProperty.isLazy()) {
            LOGGER.debugf("Attribute was declared lazy, but is part of an embeddable - `%s#%s` - LAZY will be ignored", declaringType.getNavigableRole().getFullPath(), bootProperty.getName());
        }
        fetchTiming = FetchTiming.IMMEDIATE;
        fetchStyle = FetchStyle.JOIN;
    } else {
        fetchTiming = bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE;
        fetchStyle = bootProperty.isLazy() ? FetchStyle.SELECT : FetchStyle.JOIN;
    }
    final ValueGeneration valueGeneration = bootProperty.getValueGenerationStrategy();
    if (valueConverter != null) {
        // we want to "decompose" the "type" into its various pieces as expected by the mapping
        assert valueConverter.getRelationalJavaType() == resolution.getRelationalJavaType();
        final BasicType<?> mappingBasicType = creationProcess.getCreationContext().getDomainModel().getTypeConfiguration().getBasicTypeRegistry().resolve(valueConverter.getRelationalJavaType(), resolution.getJdbcType());
        return new BasicAttributeMapping(attrName, navigableRole, stateArrayPosition, attributeMetadataAccess, fetchTiming, fetchStyle, tableExpression, attrColumnName, isAttrFormula, null, null, columnDefinition, length, precision, scale, valueConverter, mappingBasicType.getJdbcMapping(), declaringType, propertyAccess, valueGeneration);
    } else {
        return new BasicAttributeMapping(attrName, navigableRole, stateArrayPosition, attributeMetadataAccess, fetchTiming, fetchStyle, tableExpression, attrColumnName, isAttrFormula, readExpr, writeExpr, columnDefinition, length, precision, scale, null, attrType, declaringType, propertyAccess, valueGeneration);
    }
}
Also used : SortedSet(java.util.SortedSet) EntityPersister(org.hibernate.persister.entity.EntityPersister) Property(org.hibernate.mapping.Property) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) BasicType(org.hibernate.type.BasicType) CollectionIdentifierDescriptor(org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor) Any(org.hibernate.mapping.Any) Joinable(org.hibernate.persister.entity.Joinable) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) ForeignKeyDirection(org.hibernate.type.ForeignKeyDirection) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ToOne(org.hibernate.mapping.ToOne) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) PersistentClass(org.hibernate.mapping.PersistentClass) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) StandardArraySemantics(org.hibernate.collection.internal.StandardArraySemantics) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) SimpleValue(org.hibernate.mapping.SimpleValue) OneToOne(org.hibernate.mapping.OneToOne) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) StringHelper(org.hibernate.internal.util.StringHelper) Value(org.hibernate.mapping.Value) ChainedPropertyAccessImpl(org.hibernate.property.access.internal.ChainedPropertyAccessImpl) Collection(org.hibernate.mapping.Collection) Serializable(java.io.Serializable) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) List(java.util.List) OneToMany(org.hibernate.mapping.OneToMany) Dialect(org.hibernate.dialect.Dialect) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) MappingException(org.hibernate.MappingException) FetchMode(org.hibernate.FetchMode) SharedSessionContract(org.hibernate.SharedSessionContract) BasicValue(org.hibernate.mapping.BasicValue) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) StandardIdentifierBagSemantics(org.hibernate.collection.internal.StandardIdentifierBagSemantics) CollectionMappingType(org.hibernate.metamodel.mapping.CollectionMappingType) SortedMap(java.util.SortedMap) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) JavaType(org.hibernate.type.descriptor.java.JavaType) StandardBagSemantics(org.hibernate.collection.internal.StandardBagSemantics) SqlAliasStemHelper(org.hibernate.sql.ast.spi.SqlAliasStemHelper) EntityType(org.hibernate.type.EntityType) IndexedCollection(org.hibernate.mapping.IndexedCollection) ValueGeneration(org.hibernate.tuple.ValueGeneration) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ComponentType(org.hibernate.type.ComponentType) CollectionSemantics(org.hibernate.collection.spi.CollectionSemantics) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) FetchTiming(org.hibernate.engine.FetchTiming) ManyToOne(org.hibernate.mapping.ManyToOne) SortableValue(org.hibernate.mapping.SortableValue) Iterator(java.util.Iterator) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) Table(org.hibernate.mapping.Table) PropertyBasedMapping(org.hibernate.metamodel.mapping.PropertyBasedMapping) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) FetchStyle(org.hibernate.engine.FetchStyle) Component(org.hibernate.mapping.Component) CollectionClassification(org.hibernate.metamodel.CollectionClassification) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) StandardListSemantics(org.hibernate.collection.internal.StandardListSemantics) Selectable(org.hibernate.mapping.Selectable) Resolvable(org.hibernate.mapping.Resolvable) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) Map(org.hibernate.mapping.Map) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) LOGGER(org.hibernate.metamodel.mapping.MappingModelCreationLogger.LOGGER) KeyValue(org.hibernate.mapping.KeyValue) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) Resolvable(org.hibernate.mapping.Resolvable) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) BasicValue(org.hibernate.mapping.BasicValue) FetchStyle(org.hibernate.engine.FetchStyle) ValueGeneration(org.hibernate.tuple.ValueGeneration) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) FetchTiming(org.hibernate.engine.FetchTiming) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) SortableValue(org.hibernate.mapping.SortableValue) KeyValue(org.hibernate.mapping.KeyValue) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan)

Example 2 with ManagedMappingType

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

the class MappingModelCreationHelper method interpretPluralAttributeMappingKeyDescriptor.

private static void interpretPluralAttributeMappingKeyDescriptor(PluralAttributeMappingImpl attributeMapping, Collection bootValueMapping, CollectionPersister collectionDescriptor, ManagedMappingType declaringType, Dialect dialect, MappingModelCreationProcess creationProcess) {
    ModelPart attributeMappingSubPart = null;
    if (!StringHelper.isEmpty(collectionDescriptor.getMappedByProperty())) {
        attributeMappingSubPart = ((ModelPartContainer) attributeMapping.getElementDescriptor().getPartMappingType()).findSubPart(collectionDescriptor.getMappedByProperty(), null);
    }
    if (attributeMappingSubPart instanceof ToOneAttributeMapping) {
        final ToOneAttributeMapping referencedAttributeMapping = (ToOneAttributeMapping) attributeMappingSubPart;
        setReferencedAttributeForeignKeyDescriptor(attributeMapping, referencedAttributeMapping, referencedAttributeMapping.findContainingEntityMapping().getEntityPersister(), collectionDescriptor.getMappedByProperty(), dialect, creationProcess);
        return;
    }
    final KeyValue bootValueMappingKey = bootValueMapping.getKey();
    final Type keyType = bootValueMappingKey.getType();
    final ModelPart fkTarget;
    final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
    final boolean isReferenceToPrimaryKey = lhsPropertyName == null;
    final ManagedMappingType keyDeclaringType;
    if (collectionDescriptor.getElementType().isEntityType()) {
        keyDeclaringType = ((QueryableCollection) collectionDescriptor).getElementPersister();
    } else {
        // This is not "really correct" but it is as good as it gets.
        // The key declaring type serves as declaring type for the inverse model part of a FK.
        // Most of the time, there is a proper managed type, but not for basic collections.
        // Since the declaring type is needed for certain operations, we use the one from the target side of the FK
        keyDeclaringType = declaringType;
    }
    if (isReferenceToPrimaryKey) {
        fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
    } else {
        fkTarget = declaringType.findAttributeMapping(lhsPropertyName);
    }
    if (keyType instanceof BasicType) {
        assert bootValueMappingKey.getColumnSpan() == 1;
        assert fkTarget instanceof BasicValuedModelPart;
        final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget;
        final String tableExpression = getTableIdentifierExpression(bootValueMappingKey.getTable(), creationProcess);
        final SelectableMapping keySelectableMapping = SelectableMappingImpl.from(tableExpression, bootValueMappingKey.getSelectables().get(0), (JdbcMapping) keyType, dialect, creationProcess.getSqmFunctionRegistry());
        attributeMapping.setForeignKeyDescriptor(new SimpleForeignKeyDescriptor(keyDeclaringType, simpleFkTarget, null, keySelectableMapping, simpleFkTarget, isReferenceToPrimaryKey, ((SimpleValue) bootValueMappingKey).isConstrained()));
    } else if (fkTarget instanceof EmbeddableValuedModelPart) {
        final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor((EmbeddableValuedModelPart) fkTarget, bootValueMapping, keyDeclaringType, collectionDescriptor.getAttributeMapping(), false, dialect, creationProcess);
        attributeMapping.setForeignKeyDescriptor(embeddedForeignKeyDescriptor);
    } else {
        throw new NotYetImplementedFor6Exception("Support for " + fkTarget.getClass() + " foreign keys not yet implemented: " + bootValueMapping.getRole());
    }
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) KeyValue(org.hibernate.mapping.KeyValue) BasicType(org.hibernate.type.BasicType) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) SimpleValue(org.hibernate.mapping.SimpleValue) BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) CollectionMappingType(org.hibernate.metamodel.mapping.CollectionMappingType) JavaType(org.hibernate.type.descriptor.java.JavaType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 3 with ManagedMappingType

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

the class AbstractEntityPersister method getPropertyValue.

@Override
public Object getPropertyValue(Object object, String propertyName) {
    final int dotIndex = propertyName.indexOf('.');
    final String basePropertyName = dotIndex == -1 ? propertyName : propertyName.substring(0, dotIndex);
    final AttributeMapping attributeMapping = findAttributeMapping(basePropertyName);
    ManagedMappingType baseValueType = null;
    Object baseValue = null;
    if (attributeMapping != null) {
        baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
        if (dotIndex != -1) {
            baseValueType = (ManagedMappingType) attributeMapping.getMappedType();
        }
    } else if (identifierMapping instanceof NonAggregatedIdentifierMapping) {
        final EmbeddedAttributeMapping embeddedAttributeMapping = (EmbeddedAttributeMapping) findAttributeMapping(NavigableRole.IDENTIFIER_MAPPER_PROPERTY);
        final AttributeMapping mapping = embeddedAttributeMapping.getMappedType().findAttributeMapping(basePropertyName);
        if (mapping != null) {
            baseValue = mapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
            if (dotIndex != -1) {
                baseValueType = (ManagedMappingType) mapping.getMappedType();
            }
        }
    }
    return getPropertyValue(baseValue, baseValueType, propertyName, dotIndex);
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) 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)

Example 4 with ManagedMappingType

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

the class AbstractEntityPersister method getPropertyValue.

private Object getPropertyValue(Object baseValue, ManagedMappingType baseValueType, String propertyName, int dotIndex) {
    if (baseValueType == null) {
        return baseValue;
    }
    final int nextDotIndex = propertyName.indexOf('.', dotIndex + 1);
    final int endIndex = nextDotIndex == -1 ? propertyName.length() : nextDotIndex;
    final AttributeMapping attributeMapping;
    attributeMapping = baseValueType.findAttributeMapping(propertyName.substring(dotIndex + 1, endIndex));
    baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(baseValue);
    baseValueType = nextDotIndex == -1 ? null : (ManagedMappingType) attributeMapping.getMappedType();
    return getPropertyValue(baseValue, baseValueType, propertyName, nextDotIndex);
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) 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)

Aggregations

ManagedMappingType (org.hibernate.metamodel.mapping.ManagedMappingType)4 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)2 KeyValue (org.hibernate.mapping.KeyValue)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)2 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)2 CollectionMappingType (org.hibernate.metamodel.mapping.CollectionMappingType)2 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)2 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 ModelPart (org.hibernate.metamodel.mapping.ModelPart)2 SelectableMapping (org.hibernate.metamodel.mapping.SelectableMapping)2 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)2 VirtualModelPart (org.hibernate.metamodel.mapping.VirtualModelPart)2 DiscriminatedAssociationAttributeMapping (org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping)2 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)2 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)2 AssociationType (org.hibernate.type.AssociationType)2 BasicType (org.hibernate.type.BasicType)2