Search in sources :

Example 11 with AttributeMapping

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

the class AbstractCompositeIdentifierMapping method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    int span = 0;
    final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        if (attributeMapping instanceof ToOneAttributeMapping) {
            final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
            final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
            final Object identifier = fkDescriptor.getAssociationKeyFromSide(o, toOneAttributeMapping.getSideNature().inverse(), session);
            span += fkDescriptor.forEachJdbcValue(identifier, clause, span + offset, valuesConsumer, session);
        } else {
            span += attributeMapping.forEachJdbcValue(o, clause, span + offset, valuesConsumer, session);
        }
    }
    return span;
}
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)

Example 12 with AttributeMapping

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

the class DefaultLoadEventListener method checkIdClass.

private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
    // we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
    // dependent objects parent.  This is part of its generally goofy derived identity "feature"
    final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
    if (idMapping instanceof CompositeIdentifierMapping) {
        final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
        final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
        if (attributeMappings.size() == 1) {
            final AttributeMapping singleIdAttribute = attributeMappings.get(0);
            if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
                final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
                final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
                final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
                if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
                    // yep that's what we have...
                    loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
                    return;
                }
            }
        } else if (idMapping instanceof NonAggregatedIdentifierMapping) {
            if (idClass.isInstance(event.getEntityId())) {
                return;
            }
        }
    }
    throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
Also used : EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TypeMismatchException(org.hibernate.TypeMismatchException) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 13 with AttributeMapping

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

the class AbstractSqlAstTranslator method visitNullnessPredicate.

@Override
public void visitNullnessPredicate(NullnessPredicate nullnessPredicate) {
    final Expression expression = nullnessPredicate.getExpression();
    final String predicateValue;
    if (nullnessPredicate.isNegated()) {
        predicateValue = " is not null";
    } else {
        predicateValue = " is null";
    }
    final SqlTuple tuple;
    if ((tuple = SqlTupleContainer.getSqlTuple(expression)) != null) {
        String separator = NO_SEPARATOR;
        // as the embeddable is not considered as null, if at least one sub-part is not null
        if (nullnessPredicate.isNegated() && expression.getExpressionType() instanceof AttributeMapping) {
            appendSql('(');
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " or ";
            }
            appendSql(')');
        } else // For the is null check, and also for tuples in SQL in general,
        // the semantics is that all sub-parts must match the predicate
        {
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " and ";
            }
        }
    } else {
        expression.accept(this);
        appendSql(predicateValue);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Example 14 with AttributeMapping

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

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

the class AbstractEntityPersister method resolveDirtyAttributeIndexes.

@Override
public int[] resolveDirtyAttributeIndexes(final Object[] currentState, final Object[] previousState, final String[] attributeNames, final SessionImplementor session) {
    final BitSet mutablePropertiesIndexes = entityMetamodel.getMutablePropertiesIndexes();
    final int estimatedSize = attributeNames == null ? 0 : attributeNames.length + mutablePropertiesIndexes.cardinality();
    final List<Integer> fields = new ArrayList<>(estimatedSize);
    if (estimatedSize == 0) {
        return ArrayHelper.EMPTY_INT_ARRAY;
    }
    if (!mutablePropertiesIndexes.isEmpty()) {
        // We have to check the state for "mutable" properties as dirty tracking isn't aware of mutable types
        final Type[] propertyTypes = entityMetamodel.getPropertyTypes();
        final boolean[] propertyCheckability = entityMetamodel.getPropertyCheckability();
        mutablePropertiesIndexes.stream().forEach(i -> {
            // This is kindly borrowed from org.hibernate.type.TypeHelper.findDirty
            final boolean dirty = currentState[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY && // Consider mutable properties as dirty if we don't have a previous state
            (previousState == null || previousState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY || (propertyCheckability[i] && propertyTypes[i].isDirty(previousState[i], currentState[i], propertyColumnUpdateable[i], session)));
            if (dirty) {
                fields.add(i);
            }
        });
    }
    if (attributeNames.length != 0) {
        final boolean[] propertyUpdateability = entityMetamodel.getPropertyUpdateability();
        // Sort attribute names so that we can traverse mappings efficiently
        Arrays.sort(attributeNames);
        int index = 0;
        for (final AttributeMapping attributeMapping : attributeMappings) {
            final String attributeName = attributeMapping.getAttributeName();
            final int nameLength = attributeName.length();
            final String currentAttributeName = attributeNames[index];
            int position = ((StateArrayContributorMapping) attributeMapping).getStateArrayPosition();
            if (currentAttributeName.startsWith(attributeName) && ((currentAttributeName.length() == nameLength || currentAttributeName.charAt(nameLength) == '.'))) {
                if (propertyUpdateability[position] && !fields.contains(position)) {
                    fields.add(position);
                }
                index++;
                if (index < attributeNames.length) {
                    // Skip duplicates
                    do {
                        if (attributeNames[index].equals(attributeName)) {
                            index++;
                        } else {
                            break;
                        }
                    } while (index < attributeNames.length);
                } else {
                    break;
                }
            }
        }
    }
    return ArrayHelper.toIntArray(fields);
}
Also used : StateArrayContributorMapping(org.hibernate.metamodel.mapping.StateArrayContributorMapping) BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) InFlightEntityMappingType(org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType) AssociationType(org.hibernate.type.AssociationType) AnyType(org.hibernate.type.AnyType) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) JavaType(org.hibernate.type.descriptor.java.JavaType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) 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) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList)

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