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"));
}
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")]);
}
Aggregations