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