use of org.hibernate.metamodel.mapping.internal.CompoundNaturalIdMapping 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);
}
Aggregations