Search in sources :

Example 1 with CompoundNaturalIdMapping

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);
}
Also used : SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) DiscriminatedAssociationAttributeMapping(org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) ArrayList(java.util.ArrayList) CompoundNaturalIdMapping(org.hibernate.metamodel.mapping.internal.CompoundNaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) MappingException(org.hibernate.MappingException)

Aggregations

ArrayList (java.util.ArrayList)1 MappingException (org.hibernate.MappingException)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)1 CompoundNaturalIdMapping (org.hibernate.metamodel.mapping.internal.CompoundNaturalIdMapping)1 DiscriminatedAssociationAttributeMapping (org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping)1 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)1 SimpleNaturalIdMapping (org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1