Search in sources :

Example 1 with AttributeMetadataAccess

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

the class AbstractEntityPersister method getPropertyValues.

@Override
public Object[] getPropertyValues(Object object) {
    if (accessOptimizer != null) {
        return accessOptimizer.getPropertyValues(object);
    } else {
        final BytecodeEnhancementMetadata enhancementMetadata = entityMetamodel.getBytecodeEnhancementMetadata();
        final LazyAttributesMetadata lazyAttributesMetadata = enhancementMetadata.getLazyAttributesMetadata();
        final Object[] values = new Object[getNumberOfAttributeMappings()];
        for (int i = 0; i < attributeMappings.size(); i++) {
            final AttributeMapping attributeMapping = attributeMappings.get(i);
            final AttributeMetadataAccess attributeMetadataAccess = attributeMapping.getAttributeMetadataAccess();
            if (!lazyAttributesMetadata.isLazyAttribute(attributeMapping.getAttributeName()) || enhancementMetadata.isAttributeLoaded(object, attributeMapping.getAttributeName())) {
                values[i] = attributeMetadataAccess.resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
            } else {
                values[i] = LazyPropertyInitializer.UNFETCHED_PROPERTY;
            }
        }
        return values;
    }
}
Also used : 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) LazyAttributesMetadata(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata)

Example 2 with AttributeMetadataAccess

use of org.hibernate.metamodel.mapping.AttributeMetadataAccess 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 AttributeMetadataAccess attributeMetadataAccess = entityMappingType -> new AttributeMetadata() {

        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) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) 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) AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) 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) 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) NotFoundAction(org.hibernate.annotations.NotFoundAction) StandardListSemantics(org.hibernate.collection.internal.StandardListSemantics) Selectable(org.hibernate.mapping.Selectable) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) 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) 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) AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) 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) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan)

Example 3 with AttributeMetadataAccess

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

the class MappingModelCreationHelper method buildEmbeddedAttributeMapping.

public static EmbeddedAttributeMapping buildEmbeddedAttributeMapping(String attrName, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, CompositeType attrType, String tableExpression, String[] rootTableKeyColumnNames, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, MappingModelCreationProcess creationProcess) {
    final AttributeMetadataAccess attributeMetadataAccess = getAttributeMetadataAccess(bootProperty, attrType, propertyAccess, cascadeStyle, creationProcess);
    final Component component = (Component) bootProperty.getValue();
    final EmbeddableMappingTypeImpl embeddableMappingType = EmbeddableMappingTypeImpl.from(component, attrType, tableExpression, rootTableKeyColumnNames, attributeMappingType -> {
        if (component.isEmbedded()) {
            return new VirtualEmbeddedAttributeMapping(attrName, declaringType.getNavigableRole().append(attrName), stateArrayPosition, tableExpression, attributeMetadataAccess, component.getParentProperty(), FetchTiming.IMMEDIATE, FetchStyle.JOIN, attributeMappingType, declaringType, propertyAccess, bootProperty.getValueGenerationStrategy());
        } else {
            return new EmbeddedAttributeMapping(attrName, declaringType.getNavigableRole().append(attrName), stateArrayPosition, tableExpression, attributeMetadataAccess, component.getParentProperty(), FetchTiming.IMMEDIATE, FetchStyle.JOIN, attributeMappingType, declaringType, propertyAccess, bootProperty.getValueGenerationStrategy());
        }
    }, creationProcess);
    return (EmbeddedAttributeMapping) embeddableMappingType.getEmbeddedValueMapping();
}
Also used : AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) Component(org.hibernate.mapping.Component)

Example 4 with AttributeMetadataAccess

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

the class MappingModelCreationHelper method buildSingularAssociationAttributeMapping.

public static ToOneAttributeMapping buildSingularAssociationAttributeMapping(String attrName, NavigableRole navigableRole, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, EntityPersister declaringEntityPersister, EntityType attrType, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, MappingModelCreationProcess creationProcess) {
    if (bootProperty.getValue() instanceof ToOne) {
        final ToOne value = (ToOne) bootProperty.getValue();
        final EntityPersister entityPersister = creationProcess.getEntityPersister(value.getReferencedEntityName());
        final AttributeMetadataAccess attributeMetadataAccess = getAttributeMetadataAccess(bootProperty, attrType, propertyAccess, cascadeStyle, creationProcess);
        SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
        final AssociationType type = (AssociationType) bootProperty.getType();
        final FetchStyle fetchStyle = FetchOptionsHelper.determineFetchStyleByMetadata(bootProperty.getValue().getFetchMode(), type, sessionFactory);
        final FetchTiming fetchTiming;
        final String role = declaringType.getNavigableRole().toString() + "." + bootProperty.getName();
        final boolean lazy = value.isLazy();
        if (lazy && entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
            if (value.isUnwrapProxy()) {
                fetchTiming = FetchOptionsHelper.determineFetchTiming(fetchStyle, type, lazy, role, sessionFactory);
            } else if (value instanceof ManyToOne && value.isNullable() && ((ManyToOne) value).isIgnoreNotFound()) {
                fetchTiming = FetchTiming.IMMEDIATE;
            } else {
                fetchTiming = FetchOptionsHelper.determineFetchTiming(fetchStyle, type, lazy, role, sessionFactory);
            }
        } else if (!lazy || value instanceof OneToOne && value.isNullable() || value instanceof ManyToOne && value.isNullable() && ((ManyToOne) value).isIgnoreNotFound()) {
            fetchTiming = FetchTiming.IMMEDIATE;
        } else {
            fetchTiming = FetchOptionsHelper.determineFetchTiming(fetchStyle, type, lazy, role, sessionFactory);
        }
        final ToOneAttributeMapping attributeMapping = new ToOneAttributeMapping(attrName, navigableRole, stateArrayPosition, (ToOne) bootProperty.getValue(), attributeMetadataAccess, fetchTiming, fetchStyle, entityPersister, declaringType, declaringEntityPersister, propertyAccess);
        creationProcess.registerForeignKeyPostInitCallbacks("To-one key - " + navigableRole, () -> {
            final Dialect dialect = creationProcess.getCreationContext().getSessionFactory().getJdbcServices().getDialect();
            return MappingModelCreationHelper.interpretToOneKeyDescriptor(attributeMapping, bootProperty, (ToOne) bootProperty.getValue(), null, dialect, creationProcess);
        });
        return attributeMapping;
    } else {
        throw new NotYetImplementedFor6Exception("AnyType support has not yet been implemented");
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ManyToOne(org.hibernate.mapping.ManyToOne) FetchStyle(org.hibernate.engine.FetchStyle) OneToOne(org.hibernate.mapping.OneToOne) AssociationType(org.hibernate.type.AssociationType) FetchTiming(org.hibernate.engine.FetchTiming) ToOne(org.hibernate.mapping.ToOne) OneToOne(org.hibernate.mapping.OneToOne) ManyToOne(org.hibernate.mapping.ManyToOne) Dialect(org.hibernate.dialect.Dialect) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 5 with AttributeMetadataAccess

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

the class CompoundNaturalIdMapping method isMutable.

private static boolean isMutable(EntityMappingType entityDescriptor, List<SingularAttributeMapping> attributes, MappingModelCreationProcess creationProcess) {
    for (int i = 0; i < attributes.size(); i++) {
        final SingularAttributeMapping attributeMapping = attributes.get(i);
        final AttributeMetadataAccess metadataAccess = attributeMapping.getAttributeMetadataAccess();
        if (!metadataAccess.resolveAttributeMetadata(entityDescriptor).isUpdatable()) {
            return false;
        }
    }
    return true;
}
Also used : SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess)

Aggregations

AttributeMetadataAccess (org.hibernate.metamodel.mapping.AttributeMetadataAccess)8 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