use of org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method generateNaturalIdMapping.
private NaturalIdMapping generateNaturalIdMapping(MappingModelCreationProcess creationProcess, PersistentClass bootEntityDescriptor) {
assert bootEntityDescriptor.hasNaturalId();
final int[] naturalIdAttributeIndexes = entityMetamodel.getNaturalIdentifierProperties();
assert naturalIdAttributeIndexes.length > 0;
if (naturalIdAttributeIndexes.length == 1) {
final String propertyName = entityMetamodel.getPropertyNames()[naturalIdAttributeIndexes[0]];
final AttributeMapping attributeMapping = findAttributeMapping(propertyName);
return new SimpleNaturalIdMapping((SingularAttributeMapping) attributeMapping, this, creationProcess);
}
// collect the names of the attributes making up the natural-id.
final Set<String> attributeNames = CollectionHelper.setOfSize(naturalIdAttributeIndexes.length);
for (int naturalIdAttributeIndex : naturalIdAttributeIndexes) {
attributeNames.add(this.getPropertyNames()[naturalIdAttributeIndex]);
}
// then iterate over the attribute mappings finding the ones having names
// in the collected names. iterate here because it is already alphabetical
final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>();
this.attributeMappings.forEach((attributeMapping) -> {
if (attributeNames.contains(attributeMapping.getAttributeName())) {
collectedAttrMappings.add((SingularAttributeMapping) attributeMapping);
}
});
if (collectedAttrMappings.size() <= 1) {
throw new MappingException("Expected multiple natural-id attributes, but found only one: " + getEntityName());
}
return new CompoundNaturalIdMapping(this, collectedAttrMappings, creationProcess);
}
use of org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping 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 StateArrayContributorMetadata 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));
}
use of org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping 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));
}
Aggregations