Search in sources :

Example 16 with AttributeMapping

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

the class AbstractEntityPersister method generateNaturalIdMapping.

private NaturalIdMapping generateNaturalIdMapping(MappingModelCreationProcess creationProcess, PersistentClass bootEntityDescriptor) {
    assert bootEntityDescriptor.hasNaturalId();
    final int[] naturalIdAttributeIndexes = entityMetamodel.getNaturalIdentifierProperties();
    assert naturalIdAttributeIndexes.length > 0;
    if (naturalIdAttributeIndexes.length == 1) {
        final String propertyName = entityMetamodel.getPropertyNames()[naturalIdAttributeIndexes[0]];
        final AttributeMapping attributeMapping = findAttributeMapping(propertyName);
        return new SimpleNaturalIdMapping((SingularAttributeMapping) attributeMapping, this, creationProcess);
    }
    // collect the names of the attributes making up the natural-id.
    final Set<String> attributeNames = CollectionHelper.setOfSize(naturalIdAttributeIndexes.length);
    for (int naturalIdAttributeIndex : naturalIdAttributeIndexes) {
        attributeNames.add(this.getPropertyNames()[naturalIdAttributeIndex]);
    }
    // then iterate over the attribute mappings finding the ones having names
    // in the collected names.  iterate here because it is already alphabetical
    final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>();
    this.attributeMappings.forEach((attributeMapping) -> {
        if (attributeNames.contains(attributeMapping.getAttributeName())) {
            collectedAttrMappings.add((SingularAttributeMapping) attributeMapping);
        }
    });
    if (collectedAttrMappings.size() <= 1) {
        throw new MappingException("Expected multiple natural-id attributes, but found only one: " + getEntityName());
    }
    return new CompoundNaturalIdMapping(this, collectedAttrMappings, creationProcess);
}
Also used : SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) 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) ArrayList(java.util.ArrayList) CompoundNaturalIdMapping(org.hibernate.metamodel.mapping.internal.CompoundNaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) MappingException(org.hibernate.MappingException)

Example 17 with AttributeMapping

use of org.hibernate.metamodel.mapping.AttributeMapping 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 18 with AttributeMapping

use of org.hibernate.metamodel.mapping.AttributeMapping 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)

Example 19 with AttributeMapping

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

the class AbstractEntityPersister method setPropertyValue.

@Override
public void setPropertyValue(Object object, String propertyName, Object value) {
    final AttributeMapping attributeMapping = (AttributeMapping) findSubPart(propertyName, this);
    final AttributeMetadata attributeMetadata = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this);
    attributeMetadata.getPropertyAccess().getSetter().set(object, value);
}
Also used : AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) 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 20 with AttributeMapping

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

the class AttributeOrderingTests method verifyRuntimeEntityMapping.

public void verifyRuntimeEntityMapping(EntityMappingType entityMappingType) {
    final NaturalIdMapping naturalIdMapping = entityMappingType.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
    assertThat(naturalIdMapping.getNaturalIdAttributes().get(0).getAttributeName(), is("assignment"));
    assertThat(naturalIdMapping.getNaturalIdAttributes().get(1).getAttributeName(), is("userCode"));
    final ArrayList<AttributeMapping> attributeMappings = new ArrayList<>(entityMappingType.getAttributeMappings());
    assertThat(attributeMappings.size(), is(5));
    assertThat(attributeMappings.get(0).getAttributeName(), is("assignment"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[0], is("assignment"));
    assertThat(attributeMappings.get(1).getAttributeName(), is("name"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[1], is("name"));
    final EmbeddedAttributeMapping theComponentAttrMapping = (EmbeddedAttributeMapping) attributeMappings.get(2);
    assertThat(theComponentAttrMapping.getAttributeName(), is("theComponent"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[2], is("theComponent"));
    final EmbeddableMappingType embeddable = theComponentAttrMapping.getMappedType();
    final ArrayList<AttributeMapping> embeddableAttributeMappings = new ArrayList<>(embeddable.getAttributeMappings());
    assertThat(embeddableAttributeMappings.get(0).getAttributeName(), is("nestedAnything"));
    assertThat(embeddableAttributeMappings.get(1).getAttributeName(), is("nestedName"));
    assertThat(attributeMappings.get(3).getAttributeName(), is("theComponents"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[3], is("theComponents"));
    assertThat(attributeMappings.get(4).getAttributeName(), is("userCode"));
    assertThat(entityMappingType.getEntityPersister().getPropertyNames()[4], is("userCode"));
}
Also used : EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) ArrayList(java.util.ArrayList) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

Aggregations

AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)56 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)38 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)20 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)19 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)19 EntityPersister (org.hibernate.persister.entity.EntityPersister)17 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)12 DiscriminatedAssociationAttributeMapping (org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping)12 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)10 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)8 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)8 ModelPart (org.hibernate.metamodel.mapping.ModelPart)7 Expression (org.hibernate.sql.ast.tree.expression.Expression)7 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)6 NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)6 Fetchable (org.hibernate.sql.results.graph.Fetchable)6 CascadeStyle (org.hibernate.engine.spi.CascadeStyle)5 NavigablePath (org.hibernate.query.spi.NavigablePath)5