Search in sources :

Example 1 with SingularAttributeMapping

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

the class CompoundNaturalIdMapping method extractNaturalIdFromEntityState.

@Override
public Object[] extractNaturalIdFromEntityState(Object[] state, SharedSessionContractImplementor session) {
    if (state == null) {
        return null;
    }
    if (state.length == attributes.size()) {
        return state;
    }
    final Object[] values = new Object[attributes.size()];
    for (int i = 0; i <= attributes.size() - 1; i++) {
        final SingularAttributeMapping attributeMapping = attributes.get(i);
        values[i] = state[attributeMapping.getStateArrayPosition()];
    }
    return values;
}
Also used : SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 2 with SingularAttributeMapping

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

the class CompoundNaturalIdMapping method forEachJdbcValue.

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    assert value instanceof Object[];
    final Object[] incoming = (Object[]) value;
    assert incoming.length == attributes.size();
    int span = 0;
    for (int i = 0; i < attributes.size(); i++) {
        final SingularAttributeMapping attribute = attributes.get(i);
        span += attribute.forEachJdbcValue(incoming[i], clause, span + offset, valuesConsumer, session);
    }
    return span;
}
Also used : SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 3 with SingularAttributeMapping

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

the class CompoundNaturalIdMapping method forEachDisassembledJdbcValue.

@Override
public int forEachDisassembledJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    assert value instanceof Object[];
    final Object[] incoming = (Object[]) value;
    assert incoming.length == attributes.size();
    int span = 0;
    for (int i = 0; i < attributes.size(); i++) {
        final SingularAttributeMapping attribute = attributes.get(i);
        span += attribute.forEachDisassembledJdbcValue(incoming[i], clause, span + offset, valuesConsumer, session);
    }
    return span;
}
Also used : SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 4 with SingularAttributeMapping

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

the class CompoundNaturalIdMapping method verifyFlushState.

@Override
public void verifyFlushState(Object id, Object[] currentState, Object[] loadedState, SharedSessionContractImplementor session) {
    if (isMutable()) {
        // the natural id is mutable (!immutable), no need to do the checks
        return;
    }
    final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
    final EntityPersister persister = getDeclaringType().getEntityPersister();
    final Object[] naturalId = extractNaturalIdFromEntityState(currentState, session);
    final Object snapshot = loadedState == null ? persistenceContext.getNaturalIdSnapshot(id, persister) : persister.getNaturalIdMapping().extractNaturalIdFromEntityState(loadedState, session);
    final Object[] previousNaturalId = (Object[]) snapshot;
    assert naturalId.length == getNaturalIdAttributes().size();
    assert previousNaturalId.length == naturalId.length;
    for (int i = 0; i < getNaturalIdAttributes().size(); i++) {
        final SingularAttributeMapping attributeMapping = getNaturalIdAttributes().get(i);
        final boolean updatable = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(persister).isUpdatable();
        if (updatable) {
            // property is updatable (mutable), there is nothing to check
            continue;
        }
        final Object currentValue = naturalId[i];
        final Object previousValue = previousNaturalId[i];
        if (!attributeMapping.areEqual(currentValue, previousValue, session)) {
            throw new HibernateException(String.format("An immutable attribute [%s] within compound natural identifier of entity %s was altered from `%s` to `%s`", attributeMapping.getAttributeName(), persister.getEntityName(), previousValue, currentValue));
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) HibernateException(org.hibernate.HibernateException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 5 with SingularAttributeMapping

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

the class BitSetJavaTypeTests method testResolution.

@Test
public void testResolution(SessionFactoryScope scope) {
    final EntityPersister productType = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(Product.class);
    final SingularAttributeMapping bitSetAttribute = (SingularAttributeMapping) productType.findAttributeMapping("bitSet");
    // make sure BitSetTypeDescriptor was selected
    assertThat(bitSetAttribute.getJavaType(), instanceOf(BitSetJavaType.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) Test(org.junit.jupiter.api.Test)

Aggregations

SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)11 Test (org.junit.jupiter.api.Test)10 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)6 NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)5 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)4 TestForIssue (org.hibernate.testing.TestForIssue)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)3 SimpleNaturalIdMapping (org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping)3 EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)3 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)2 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)2 StateArrayContributorMetadataAccess (org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess)2 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HibernateException (org.hibernate.HibernateException)1 MappingException (org.hibernate.MappingException)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1