Search in sources :

Example 6 with SingularAttributeMapping

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

the class AbstractCompositeIdAndNaturalIdTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
    final EntityMappingType accountMapping = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Account.class);
    final SingularAttributeMapping shortCodeMapping = ((SimpleNaturalIdMapping) accountMapping.getNaturalIdMapping()).getAttribute();
    final AttributeMetadata shortCodeMetadata = shortCodeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertThat(shortCodeMetadata.isNullable(), is(false));
    final EntityPersister rootEntityPersister = accountMapping.getRootEntityDescriptor().getEntityPersister();
    final int shortCodeLegacyPropertyIndex = rootEntityPersister.getEntityMetamodel().getPropertyIndex("shortCode");
    assertThat(shortCodeLegacyPropertyIndex, is(0));
    assertThat(rootEntityPersister.getPropertyNullability()[shortCodeLegacyPropertyIndex], is(false));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 7 with SingularAttributeMapping

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

the class ImmutableManyToOneNaturalIdAnnotationTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
    // nullability is not specified for either properties making up
    // the natural ID, so they should be nullable by annotation-specific default
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final EntityMappingType childMapping = runtimeMetamodels.getEntityMappingType(Child.class.getName());
    final EntityPersister persister = childMapping.getEntityPersister();
    final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
    final int nameIndex = entityMetamodel.getPropertyIndex("name");
    final int parentIndex = entityMetamodel.getPropertyIndex("parent");
    // checking alphabetic sort in relation to EntityPersister/EntityMetamodel
    assertThat(nameIndex, lessThan(parentIndex));
    assertFalse(persister.getPropertyUpdateability()[nameIndex]);
    assertFalse(persister.getPropertyUpdateability()[parentIndex]);
    assertTrue(persister.getPropertyNullability()[nameIndex]);
    assertTrue(persister.getPropertyNullability()[parentIndex]);
    final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
    assertNotNull(naturalIdMapping);
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
    // access by list-index should again be alphabetically sorted
    final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
    assertThat(first.getAttributeName(), is("name"));
    final AttributeMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(firstMetadata.getMutabilityPlan().isMutable());
    final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
    assertThat(second.getAttributeName(), is("parent"));
    final AttributeMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 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 9 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 10 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)

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 AttributeMetadata (org.hibernate.metamodel.mapping.AttributeMetadata)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 AttributeMetadataAccess (org.hibernate.metamodel.mapping.AttributeMetadataAccess)2 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)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