Search in sources :

Example 21 with SingularAttributeMapping

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

the class InheritedNaturalIdTest method verifyMappingModel.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void verifyMappingModel(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final EntityMappingType userMapping = sessionFactory.getRuntimeMetamodels().getEntityMappingType(User.class);
    final SingularAttributeMapping uidMapping = ((SimpleNaturalIdMapping) userMapping.getNaturalIdMapping()).getAttribute();
    assertThat(uidMapping.getAttributeName(), is("uid"));
    final StateArrayContributorMetadata uidMetadata = uidMapping.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertThat(uidMetadata.isNullable(), is(true));
    final EntityPersister rootEntityPersister = userMapping.getEntityPersister();
    final int uidLegacyPropertyIndex = rootEntityPersister.getEntityMetamodel().getPropertyIndex("uid");
    assertThat(uidLegacyPropertyIndex, is(0));
    assertThat(rootEntityPersister.getPropertyNullability()[uidLegacyPropertyIndex], is(true));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 22 with SingularAttributeMapping

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

the class ImmutableEntityNaturalIdTest method testNaturalIdMapping.

@Test
public void testNaturalIdMapping(SessionFactoryScope scope) {
    final EntityMappingType buildingMapping = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Building.class);
    final NaturalIdMapping naturalIdMapping = buildingMapping.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(3));
    // nullability is not specified, so they should be nullable by annotations-specific default
    for (SingularAttributeMapping attribute : naturalIdMapping.getNaturalIdAttributes()) {
        assertThat(attribute.getAttributeMetadataAccess().resolveAttributeMetadata(null).isNullable(), is(true));
    }
    final EntityPersister entityPersister = buildingMapping.getEntityPersister();
    assertThat("Class should have a natural key", entityPersister.hasNaturalIdentifier(), is(true));
    final EntityMetamodel entityMetamodel = entityPersister.getEntityMetamodel();
    assertThat("Wrong number of attributes", entityMetamodel.getNaturalIdentifierProperties().length, is(3));
    // nullability is not specified, so they should be nullable by annotations-specific default
    assertTrue(entityPersister.getPropertyNullability()[entityMetamodel.getPropertyIndex("address")]);
    assertTrue(entityPersister.getPropertyNullability()[entityMetamodel.getPropertyIndex("city")]);
    assertTrue(entityPersister.getPropertyNullability()[entityMetamodel.getPropertyIndex("state")]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) 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