Search in sources :

Example 21 with NaturalIdMapping

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

the class CompoundNaturalIdTests method testProcessing.

@Test
public void testProcessing(DomainModelScope domainModelScope, SessionFactoryScope factoryScope) {
    final PersistentClass accountBootMapping = domainModelScope.getDomainModel().getEntityBinding(Account.class.getName());
    assertThat(accountBootMapping.hasNaturalId(), is(true));
    final Property username = accountBootMapping.getProperty("username");
    assertThat(username.isNaturalIdentifier(), is(true));
    final Property system = accountBootMapping.getProperty("system");
    assertThat(system.isNaturalIdentifier(), is(true));
    final MappingMetamodel mappingMetamodel = factoryScope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister accountMapping = mappingMetamodel.findEntityDescriptor(Account.class);
    assertThat(accountMapping.hasNaturalIdentifier(), is(true));
    final NaturalIdMapping naturalIdMapping = accountMapping.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
    final List<SingularAttributeMapping> attributes = naturalIdMapping.getNaturalIdAttributes();
    assertThat(attributes.size(), is(2));
    // alphabetical matching overall processing
    final SingularAttributeMapping first = attributes.get(0);
    assertThat(first, notNullValue());
    assertThat(first.getAttributeName(), is("system"));
    final SingularAttributeMapping second = attributes.get(1);
    assertThat(second, notNullValue());
    assertThat(second.getAttributeName(), is("username"));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test)

Example 22 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping 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

NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Test (org.junit.jupiter.api.Test)8 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)5 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)5 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)4 EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)3 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 Property (org.hibernate.mapping.Property)2 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)2 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1