Search in sources :

Example 11 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.

the class CompoundNaturalIdTests method testLoad.

@Test
public void testLoad(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final Account account = session.byNaturalId(Account.class).using("system", "matrix").using("username", "neo").load();
        verifyEntity(account);
    });
    scope.inTransaction(session -> {
        final MappingMetamodel mappingMetamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
        final EntityPersister accountMapping = mappingMetamodel.findEntityDescriptor(Account.class);
        final NaturalIdMapping naturalIdMapping = accountMapping.getNaturalIdMapping();
        // test load by array
        accountMapping.getNaturalIdLoader().load(VALUE_ARRAY, NaturalIdLoadOptions.NONE, session);
        // and by Map
        accountMapping.getNaturalIdLoader().load(VALUE_NAP, NaturalIdLoadOptions.NONE, session);
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) Test(org.junit.jupiter.api.Test)

Example 12 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.

the class CompoundNaturalIdTests method testGetReference.

@Test
public void testGetReference(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final NaturalIdLoadAccess<Account> loadAccess = session.byNaturalId(Account.class);
        loadAccess.using("system", "matrix");
        loadAccess.using("username", "neo");
        verifyEntity(loadAccess.getReference());
    });
    scope.inTransaction(session -> {
        final MappingMetamodel mappingMetamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
        final EntityPersister accountMapping = mappingMetamodel.findEntityDescriptor(Account.class);
        final NaturalIdMapping naturalIdMapping = accountMapping.getNaturalIdMapping();
        // test load by array
        Object id = accountMapping.getNaturalIdLoader().resolveNaturalIdToId(VALUE_ARRAY, session);
        assertThat(id, is(1));
        // and by Map
        id = accountMapping.getNaturalIdLoader().resolveNaturalIdToId(VALUE_NAP, session);
        assertThat(id, is(1));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) Test(org.junit.jupiter.api.Test)

Example 13 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.

the class SimpleNaturalIdTests method testProcessing.

@Test
public void testProcessing(DomainModelScope domainModelScope, SessionFactoryScope factoryScope) {
    final PersistentClass productBootMapping = domainModelScope.getDomainModel().getEntityBinding(Product.class.getName());
    assertThat(productBootMapping.hasNaturalId(), is(true));
    final Property sku = productBootMapping.getProperty("sku");
    assertThat(sku.isNaturalIdentifier(), is(true));
    final MappingMetamodel mappingMetamodel = factoryScope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister productMapping = mappingMetamodel.findEntityDescriptor(Product.class);
    assertThat(productMapping.hasNaturalIdentifier(), is(true));
    final NaturalIdMapping naturalIdMapping = productMapping.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) Product(org.hibernate.testing.orm.domain.retail.Product) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test)

Example 14 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.

the class ImmutableManyToOneNaturalIdAnnotationTest method testNaturalIdNullability.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
    // nullability is not specified for either properties making up
    // the natural ID, so they should be nullable by annotation-specific default
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final EntityMappingType childMapping = runtimeMetamodels.getEntityMappingType(Child.class.getName());
    final EntityPersister persister = childMapping.getEntityPersister();
    final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
    final int nameIndex = entityMetamodel.getPropertyIndex("name");
    final int parentIndex = entityMetamodel.getPropertyIndex("parent");
    // checking alphabetic sort in relation to EntityPersister/EntityMetamodel
    assertThat(nameIndex, lessThan(parentIndex));
    assertFalse(persister.getPropertyUpdateability()[nameIndex]);
    assertFalse(persister.getPropertyUpdateability()[parentIndex]);
    assertTrue(persister.getPropertyNullability()[nameIndex]);
    assertTrue(persister.getPropertyNullability()[parentIndex]);
    final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
    assertNotNull(naturalIdMapping);
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
    // access by list-index should again be alphabetically sorted
    final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
    assertThat(first.getAttributeName(), is("name"));
    final StateArrayContributorMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(firstMetadata.getMutabilityPlan().isMutable());
    final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
    assertThat(second.getAttributeName(), is("parent"));
    final StateArrayContributorMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with NaturalIdMapping

use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.

the class AbstractEntityInsertAction method handleNaturalIdPostSaveNotifications.

/**
 * Handle sending notifications needed for natural-id after saving
 *
 * @param generatedId The generated entity identifier
 */
public void handleNaturalIdPostSaveNotifications(Object generatedId) {
    final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping();
    if (naturalIdMapping == null) {
        return;
    }
    final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState(state, getSession());
    if (isEarlyInsert()) {
        // with early insert, we still need to add a local (transactional) natural id cross-reference
        getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(generatedId, naturalIdValues, getPersister(), CachedNaturalIdValueSource.INSERT);
    }
    // after save, we need to manage the shared cache entries
    getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution(generatedId, naturalIdValues, null, getPersister(), CachedNaturalIdValueSource.INSERT);
}
Also used : NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Aggregations

NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Test (org.junit.jupiter.api.Test)8 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)5 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)5 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)4 EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)3 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 Property (org.hibernate.mapping.Property)2 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)2 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1