Search in sources :

Example 41 with AttributeMapping

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

the class AbstractEmbeddableInitializer method applyMapsId.

private void applyMapsId(RowProcessingState processingState) {
    final SharedSessionContractImplementor session = processingState.getSession();
    if (embedded instanceof CompositeIdentifierMapping) {
        final CompositeIdentifierMapping cid = (CompositeIdentifierMapping) embedded;
        final EmbeddableMappingType mappedIdEmbeddable = cid.getMappedIdEmbeddableTypeDescriptor();
        if (cid.hasContainingClass()) {
            final EmbeddableMappingType virtualIdEmbeddable = embedded.getEmbeddableTypeDescriptor();
            if (virtualIdEmbeddable == mappedIdEmbeddable) {
                return;
            }
            virtualIdEmbeddable.forEachAttributeMapping((position, virtualIdAttribute) -> {
                final AttributeMapping mappedIdAttribute = mappedIdEmbeddable.getAttributeMapping(position);
                if (virtualIdAttribute instanceof ToOneAttributeMapping && !(mappedIdAttribute instanceof ToOneAttributeMapping)) {
                    final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
                    final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
                    final Object associationKey = fkDescriptor.getAssociationKeyFromSide(rowState[position], toOneAttributeMapping.getSideNature().inverse(), session);
                    rowState[position] = associationKey;
                }
            });
        }
    }
}
Also used : ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

Example 42 with AttributeMapping

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

the class EmbeddedComponentType method isMethodOf.

@Override
public boolean isMethodOf(Method method) {
    if (mappingModelPart() == null) {
        throw new IllegalStateException("EmbeddableValuedModelPart not known yet");
    }
    final EmbeddableMappingType embeddable = mappingModelPart().getEmbeddableTypeDescriptor();
    for (int i = 0; i < embeddable.getAttributeMappings().size(); i++) {
        final AttributeMapping attributeMapping = embeddable.getAttributeMapping(i);
        final Getter getter = attributeMapping.getPropertyAccess().getGetter();
        final Method getterMethod = getter.getMethod();
        if (getterMethod != null && getterMethod.equals(method)) {
            return true;
        }
    }
    return false;
}
Also used : Getter(org.hibernate.property.access.spi.Getter) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) Method(java.lang.reflect.Method) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

Example 43 with AttributeMapping

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

Example 44 with AttributeMapping

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

the class NonAggregatedIdentifierMappingImpl method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    if (hasContainingClass()) {
        final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
        final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
        final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
        final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
        int offset = 0;
        for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
            offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
                final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
                final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
                columnReferences.add((ColumnReference) columnReference);
            });
        }
        return new SqlTuple(columnReferences, this);
    }
    return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) RootClass(org.hibernate.mapping.RootClass) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) Clause(org.hibernate.sql.ast.Clause) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) BiConsumer(java.util.function.BiConsumer) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) EntityKey(org.hibernate.engine.spi.EntityKey) NavigablePath(org.hibernate.spi.NavigablePath) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) List(java.util.List) AbstractCompositeIdentifierMapping(org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping) Component(org.hibernate.mapping.Component) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) NavigablePath(org.hibernate.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 45 with AttributeMapping

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

the class NonAggregatedIdentifierMappingImpl method setIdentifier.

@Override
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
    final List<AttributeMapping> mappedIdAttributeMappings = identifierValueMapper.getAttributeMappings();
    final Object[] propertyValues = new Object[mappedIdAttributeMappings.size()];
    getEmbeddableTypeDescriptor().forEachAttributeMapping((position, attribute) -> {
        final AttributeMapping mappedIdAttributeMapping = mappedIdAttributeMappings.get(position);
        final Object o = mappedIdAttributeMapping.getPropertyAccess().getGetter().get(id);
        if (attribute instanceof ToOneAttributeMapping && !(mappedIdAttributeMapping instanceof ToOneAttributeMapping)) {
            final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute;
            final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType().getEntityPersister();
            final EntityKey entityKey = session.generateEntityKey(o, entityPersister);
            final PersistenceContext persistenceContext = session.getPersistenceContext();
            // it is conceivable there is a proxy, so check that first
            propertyValues[position] = persistenceContext.getProxy(entityKey);
            if (propertyValues[position] == null) {
                // otherwise look for an initialized version
                propertyValues[position] = persistenceContext.getEntity(entityKey);
                if (propertyValues[position] == null) {
                    // get the association out of the entity itself
                    propertyValues[position] = entityDescriptor.getPropertyValue(entity, toOneAttributeMapping.getAttributeName());
                }
            }
        } else {
            propertyValues[position] = o;
        }
    });
    getEmbeddableTypeDescriptor().setValues(entity, propertyValues);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)64 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)40 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)21 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)21 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)20 EntityPersister (org.hibernate.persister.entity.EntityPersister)19 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)14 DiscriminatedAssociationAttributeMapping (org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping)13 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)12 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)11 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)10 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)9 ModelPart (org.hibernate.metamodel.mapping.ModelPart)8 NavigablePath (org.hibernate.spi.NavigablePath)8 Expression (org.hibernate.sql.ast.tree.expression.Expression)8 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)8 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)7 NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)7 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)7