Search in sources :

Example 1 with NaturalIdMapping

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

the class NaturalIdResolutionsImpl method handleSynchronization.

@Override
public void handleSynchronization(Object pk, Object entity, EntityMappingType entityDescriptor) {
    final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
    if (naturalIdMapping == null) {
        // nothing to do
        return;
    }
    final EntityPersister persister = locatePersisterForKey(entityDescriptor.getEntityPersister());
    final Object naturalIdValuesFromCurrentObjectState = naturalIdMapping.extractNaturalIdFromEntity(entity, session());
    final boolean changed = !sameAsCached(persister, pk, naturalIdValuesFromCurrentObjectState);
    if (changed) {
        final Object cachedNaturalIdValues = findCachedNaturalIdById(pk, persister);
        cacheResolution(pk, naturalIdValuesFromCurrentObjectState, persister);
        stashInvalidNaturalIdReference(persister, cachedNaturalIdValues);
        removeSharedResolution(pk, cachedNaturalIdValues, persister);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 2 with NaturalIdMapping

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

the class NaturalIdResolutionsImpl method removeSharedResolution.

@Override
public void removeSharedResolution(Object id, Object naturalId, EntityMappingType entityDescriptor) {
    final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
    if (naturalIdMapping == null) {
        // nothing to do
        return;
    }
    final NaturalIdDataAccess cacheAccess = naturalIdMapping.getCacheAccess();
    if (cacheAccess == null) {
        // nothing to do
        return;
    }
    // todo : couple of things wrong here:
    // 1) should be using access strategy, not plain evict..
    // 2) should prefer session-cached values if any (requires interaction from removeLocalNaturalIdCrossReference)
    final EntityPersister persister = locatePersisterForKey(entityDescriptor.getEntityPersister());
    final Object naturalIdCacheKey = cacheAccess.generateCacheKey(naturalId, persister, session());
    cacheAccess.evict(naturalIdCacheKey);
// if ( sessionCachedNaturalIdValues != null
// && !Arrays.equals( sessionCachedNaturalIdValues, deletedNaturalIdValues ) ) {
// final NaturalIdCacheKey sessionNaturalIdCacheKey = new NaturalIdCacheKey( sessionCachedNaturalIdValues, persister, session );
// cacheAccess.evict( sessionNaturalIdCacheKey );
// }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess)

Example 3 with NaturalIdMapping

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

the class NaturalIdResolutionsImpl method manageLocalResolution.

@Override
public void manageLocalResolution(Object id, Object naturalIdValue, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source) {
    final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
    if (naturalIdMapping == null) {
        // nothing to do
        return;
    }
    cacheResolutionLocally(id, naturalIdValue, entityDescriptor);
}
Also used : NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 4 with NaturalIdMapping

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

the class NaturalIdResolutionsImpl method manageSharedResolution.

@Override
public void manageSharedResolution(final Object id, Object naturalId, Object previousNaturalId, EntityMappingType entityDescriptor, CachedNaturalIdValueSource source) {
    final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
    if (naturalIdMapping == null) {
        // nothing to do
        return;
    }
    if (naturalIdMapping.getCacheAccess() == null) {
        // nothing to do
        return;
    }
    manageSharedResolution(entityDescriptor.getEntityPersister(), id, naturalId, previousNaturalId, source);
}
Also used : NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping)

Example 5 with NaturalIdMapping

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

the class NaturalIdResolutionsImpl method cacheResolutionFromLoad.

@Override
public void cacheResolutionFromLoad(Object id, Object naturalId, EntityMappingType entityDescriptor) {
    NaturalIdLogging.LOGGER.debugf("Caching resolution natural-id resolution from load (%s) : `%s` -> `%s`", entityDescriptor.getEntityName(), naturalId, id);
    final EntityPersister persister = locatePersisterForKey(entityDescriptor.getEntityPersister());
    final NaturalIdMapping naturalIdMapping = entityDescriptor.getNaturalIdMapping();
    if (naturalIdMapping == null) {
        // nothing to do
        return;
    }
    // 'justAddedLocally' is meant to handle the case where we would get double stats journaling
    // from a single load event.  The first put journal would come from the natural id resolution;
    // the second comes from the entity loading.  In this condition, we want to avoid the multiple
    // 'put' stats incrementing.
    final boolean justAddedLocally = cacheResolution(id, naturalId, entityDescriptor);
    if (justAddedLocally && naturalIdMapping.getCacheAccess() != null) {
        manageSharedResolution(persister, id, naturalId, (Object) null, CachedNaturalIdValueSource.LOAD);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) 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