Search in sources :

Example 6 with NaturalIdMapping

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());
}
Also used : NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 7 with NaturalIdMapping

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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) Resolution(org.hibernate.engine.spi.Resolution)

Example 8 with NaturalIdMapping

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;
}
Also used : NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 9 with NaturalIdMapping

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;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 10 with NaturalIdMapping

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"));
}
Also used : EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) ArrayList(java.util.ArrayList) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType)

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