use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.
the class NaturalIdResolutionsImpl method validateNaturalId.
/**
* Invariant validate of the natural id. Checks include<ul>
* <li>that the entity defines a natural id</li>
* <li>the number of natural id values matches the expected number</li>
* </ul>
*
* @param entityDescriptor The entity type descriptor
* @param naturalIdValues The natural id values
*/
protected void validateNaturalId(EntityMappingType entityDescriptor, Object naturalIdValues) {
final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
if (naturalIdMapping == null) {
throw new IllegalArgumentException("Entity did not define a natural-id");
}
naturalIdMapping.validateInternalForm(naturalIdValues, persistenceContext.getSession());
}
use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.
the class NaturalIdResolutionsImpl method removeResolution.
@Override
public Object removeResolution(Object id, Object naturalId, EntityMappingType entityDescriptor) {
final EntityPersister persister = locatePersisterForKey(entityDescriptor.getEntityPersister());
final NaturalIdMapping naturalIdMapping = persister.getNaturalIdMapping();
validateNaturalId(persister, naturalId);
final EntityResolutions entityNaturalIdResolutionCache = resolutionsByEntity.get(persister);
Object sessionCachedNaturalIdValues = null;
if (entityNaturalIdResolutionCache != null) {
final Resolution cachedNaturalId = entityNaturalIdResolutionCache.pkToNaturalIdMap.remove(id);
if (cachedNaturalId != null) {
entityNaturalIdResolutionCache.naturalIdToPkMap.remove(cachedNaturalId);
sessionCachedNaturalIdValues = cachedNaturalId.getNaturalIdValue();
}
}
if (persister.hasNaturalIdCache()) {
final NaturalIdDataAccess naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy();
final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey(naturalId, persister, session());
naturalIdCacheAccessStrategy.evict(naturalIdCacheKey);
if (sessionCachedNaturalIdValues != null && !naturalIdMapping.areEqual(sessionCachedNaturalIdValues, naturalId, session())) {
final Object sessionNaturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey(sessionCachedNaturalIdValues, persister, session());
naturalIdCacheAccessStrategy.evict(sessionNaturalIdCacheKey);
}
}
return sessionCachedNaturalIdValues;
}
use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.
the class NaturalIdResolutionsImpl method isValidValue.
private boolean isValidValue(Object naturalIdValues, EntityMappingType entityDescriptor) {
final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
if (naturalIdMapping == null) {
throw new IllegalArgumentException("Entity did not define a natural-id");
}
naturalIdMapping.validateInternalForm(naturalIdValues, persistenceContext.getSession());
// validateInternalForm would have thrown an exception if not
return true;
}
use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.
the class NaturalIdResolutionsImpl method removeLocalResolution.
@Override
public Object removeLocalResolution(Object id, Object naturalId, EntityMappingType entityDescriptor) {
NaturalIdLogging.LOGGER.debugf("Removing locally cached natural-id resolution (%s) : `%s` -> `%s`", entityDescriptor.getEntityName(), naturalId, id);
final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
if (naturalIdMapping == null) {
// nothing to do
return null;
}
final EntityPersister persister = locatePersisterForKey(entityDescriptor.getEntityPersister());
final Object localNaturalIdValues = removeNaturalIdCrossReference(id, naturalId, persister);
return localNaturalIdValues != null ? localNaturalIdValues : naturalId;
}
use of org.hibernate.metamodel.mapping.NaturalIdMapping in project hibernate-orm by hibernate.
the class AttributeOrderingTests method verifyRuntimeEntityMapping.
public void verifyRuntimeEntityMapping(EntityMappingType entityMappingType) {
final NaturalIdMapping naturalIdMapping = entityMappingType.getNaturalIdMapping();
assertThat(naturalIdMapping, notNullValue());
assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
assertThat(naturalIdMapping.getNaturalIdAttributes().get(0).getAttributeName(), is("assignment"));
assertThat(naturalIdMapping.getNaturalIdAttributes().get(1).getAttributeName(), is("userCode"));
final ArrayList<AttributeMapping> attributeMappings = new ArrayList<>(entityMappingType.getAttributeMappings());
assertThat(attributeMappings.size(), is(5));
assertThat(attributeMappings.get(0).getAttributeName(), is("assignment"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[0], is("assignment"));
assertThat(attributeMappings.get(1).getAttributeName(), is("name"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[1], is("name"));
final EmbeddedAttributeMapping theComponentAttrMapping = (EmbeddedAttributeMapping) attributeMappings.get(2);
assertThat(theComponentAttrMapping.getAttributeName(), is("theComponent"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[2], is("theComponent"));
final EmbeddableMappingType embeddable = theComponentAttrMapping.getMappedType();
final ArrayList<AttributeMapping> embeddableAttributeMappings = new ArrayList<>(embeddable.getAttributeMappings());
assertThat(embeddableAttributeMappings.get(0).getAttributeName(), is("nestedAnything"));
assertThat(embeddableAttributeMappings.get(1).getAttributeName(), is("nestedName"));
assertThat(attributeMappings.get(3).getAttributeName(), is("theComponents"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[3], is("theComponents"));
assertThat(attributeMappings.get(4).getAttributeName(), is("userCode"));
assertThat(entityMappingType.getEntityPersister().getPropertyNames()[4], is("userCode"));
}
Aggregations