Search in sources :

Example 1 with SimpleNaturalIdMapping

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);
}
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)

Example 2 with SimpleNaturalIdMapping

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));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with SimpleNaturalIdMapping

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));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SimpleNaturalIdMapping(org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)3 SimpleNaturalIdMapping (org.hibernate.metamodel.mapping.internal.SimpleNaturalIdMapping)3 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 MappingException (org.hibernate.MappingException)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)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 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1