Search in sources :

Example 36 with AttributeMapping

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

the class EmbeddableMappingTypeImpl method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
    int span = 0;
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        if (attributeMapping instanceof PluralAttributeMapping) {
            continue;
        }
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        span += attributeMapping.forEachJdbcValue(o, clause, span + offset, consumer, session);
    }
    return span;
}
Also used : PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping)

Example 37 with AttributeMapping

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

the class EmbeddableMappingTypeImpl method breakDownJdbcValues.

@Override
public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) {
    if (domainValue instanceof Object[]) {
        final Object[] values = (Object[]) domainValue;
        assert values.length == attributeMappings.size();
        for (int i = 0; i < attributeMappings.size(); i++) {
            final AttributeMapping attributeMapping = attributeMappings.get(i);
            final Object attributeValue = values[i];
            attributeMapping.breakDownJdbcValues(attributeValue, valueConsumer, session);
        }
    } else {
        attributeMappings.forEach((attributeMapping) -> {
            final Object attributeValue = attributeMapping.getPropertyAccess().getGetter().get(domainValue);
            attributeMapping.breakDownJdbcValues(attributeValue, valueConsumer, session);
        });
    }
}
Also used : PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping)

Example 38 with AttributeMapping

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

the class IdClassEmbeddable method getIdentifier.

@Override
public Object getIdentifier(Object entity, SharedSessionContractImplementor session) {
    final Object id = representationStrategy.getInstantiator().instantiate(null, sessionFactory);
    final List<AttributeMapping> virtualIdAttribute = virtualIdEmbeddable.getAttributeMappings();
    final List<AttributeMapping> idClassAttribute = getAttributeMappings();
    final Object[] propertyValues = new Object[virtualIdAttribute.size()];
    for (int i = 0; i < propertyValues.length; i++) {
        final AttributeMapping attributeMapping = virtualIdAttribute.get(i);
        final Object o = attributeMapping.getPropertyAccess().getGetter().get(entity);
        if (o == null) {
            final AttributeMapping idClassAttributeMapping = idClassAttribute.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 && !(idClassAttribute.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;
        }
    }
    setValues(id, propertyValues);
    return id;
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping)

Example 39 with AttributeMapping

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

the class IdClassEmbeddable method disassemble.

@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
    final List<AttributeMapping> attributeMappings = getAttributeMappings();
    // todo (6.0) : reduce to-one values to id here?
    final Object[] result = new Object[attributeMappings.size()];
    for (int i = 0; i < attributeMappings.size(); i++) {
        final AttributeMapping attributeMapping = attributeMappings.get(i);
        Object o = attributeMapping.getPropertyAccess().getGetter().get(value);
        result[i] = attributeMapping.disassemble(o, session);
    }
    return result;
}
Also used : PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping)

Example 40 with AttributeMapping

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

the class IdClassEmbeddable method setIdentifier.

@Override
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor factory = session.getFactory();
    final EntityPersister entityDescriptor = factory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entity.getClass());
    final Object[] propertyValues = new Object[attributeMappings.size()];
    virtualIdEmbeddable.forEachAttribute((position, virtualIdAttribute) -> {
        final AttributeMapping idClassAttribute = attributeMappings.get(position);
        final Object o = idClassAttribute.getPropertyAccess().getGetter().get(id);
        if (virtualIdAttribute instanceof ToOneAttributeMapping && !(idClassAttribute instanceof ToOneAttributeMapping)) {
            final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
            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;
        }
    });
    virtualIdEmbeddable.setValues(entity, propertyValues);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

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