Search in sources :

Example 11 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy in project hibernate-orm by hibernate.

the class DefaultRefreshEventListener method evictCachedCollections.

private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException {
    for (Type type : types) {
        if (type.isCollectionType()) {
            CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister(((CollectionType) type).getRole());
            if (collectionPersister.hasCache()) {
                final CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy();
                final Object ck = cache.generateCacheKey(id, collectionPersister, source.getFactory(), source.getTenantIdentifier());
                final SoftLock lock = cache.lockItem(source, ck, null);
                source.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

                    @Override
                    public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                        cache.unlockItem(session, ck, lock);
                    }
                });
                cache.remove(source, ck);
            }
        } else if (type.isComponentType()) {
            CompositeType actype = (CompositeType) type;
            evictCachedCollections(actype.getSubtypes(), id, source);
        }
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock) CompositeType(org.hibernate.type.CompositeType)

Example 12 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy in project hibernate-orm by hibernate.

the class SessionFactoryImplementor method getSecondLevelCacheRegionAccessStrategy.

/**
	 * Find the "access strategy" for the named cache region.
	 *
	 * @param regionName The name of the region
	 *
	 * @return That region's "access strategy"
	 *
	 *
	 * @deprecated (since 5.2) Use this factory's {@link #getCache()} reference
	 * to access {@link CacheImplementor#determineEntityRegionAccessStrategy} or
	 * {@link CacheImplementor#determineCollectionRegionAccessStrategy} instead.
	 */
@Deprecated
default default RegionAccessStrategy getSecondLevelCacheRegionAccessStrategy(String regionName) {
    final EntityRegionAccessStrategy entityRegionAccess = getCache().getEntityRegionAccess(regionName);
    if (entityRegionAccess != null) {
        return entityRegionAccess;
    }
    final CollectionRegionAccessStrategy collectionRegionAccess = getCache().getCollectionRegionAccess(regionName);
    if (collectionRegionAccess != null) {
        return collectionRegionAccess;
    }
    return null;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 13 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy in project hibernate-orm by hibernate.

the class SessionFactoryImplementor method getSecondLevelCacheRegion.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Move to CacheImplementor calls
/**
	 * Get a named second-level cache region
	 *
	 * @param regionName The name of the region to retrieve.
	 *
	 * @return The name of the region
	 *
	 * @deprecated (since 5.2) Use this factory's {@link #getCache()} reference
	 * to access Region via {@link CacheImplementor#determineEntityRegionAccessStrategy} or
	 * {@link CacheImplementor#determineCollectionRegionAccessStrategy} instead.
	 */
@Deprecated
default default Region getSecondLevelCacheRegion(String regionName) {
    final EntityRegionAccessStrategy entityRegionAccess = getCache().getEntityRegionAccess(regionName);
    if (entityRegionAccess != null) {
        return entityRegionAccess.getRegion();
    }
    final CollectionRegionAccessStrategy collectionRegionAccess = getCache().getCollectionRegionAccess(regionName);
    if (collectionRegionAccess != null) {
        return collectionRegionAccess.getRegion();
    }
    return null;
}
Also used : EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 14 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy in project hibernate-orm by hibernate.

the class CollectionAction method beforeExecutions.

@Override
public final void beforeExecutions() throws CacheException {
    // the database (this action is responsible for second-level cache invalidation only)
    if (persister.hasCache()) {
        final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(key, persister, session.getFactory(), session.getTenantIdentifier());
        final SoftLock lock = cache.lockItem(session, ck, null);
        // the old behavior used key as opposed to getKey()
        afterTransactionProcess = new CacheCleanupProcess(key, persister, lock);
    }
}
Also used : CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock)

Example 15 with CollectionRegionAccessStrategy

use of org.hibernate.cache.spi.access.CollectionRegionAccessStrategy in project hibernate-orm by hibernate.

the class MetamodelImpl method initialize.

/**
	 * Prepare the metamodel using the information from the collection of Hibernate
	 * {@link PersistentClass} models
	 *
	 * @param mappingMetadata The mapping information
	 * @param jpaMetaModelPopulationSetting Should the JPA Metamodel be built as well?
	 */
public void initialize(MetadataImplementor mappingMetadata, JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting) {
    this.imports.putAll(mappingMetadata.getImports());
    final PersisterCreationContext persisterCreationContext = new PersisterCreationContext() {

        @Override
        public SessionFactoryImplementor getSessionFactory() {
            return sessionFactory;
        }

        @Override
        public MetadataImplementor getMetadata() {
            return mappingMetadata;
        }
    };
    final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService(PersisterFactory.class);
    for (final PersistentClass model : mappingMetadata.getEntityBindings()) {
        final EntityRegionAccessStrategy accessStrategy = sessionFactory.getCache().determineEntityRegionAccessStrategy(model);
        final NaturalIdRegionAccessStrategy naturalIdAccessStrategy = sessionFactory.getCache().determineNaturalIdRegionAccessStrategy(model);
        final EntityPersister cp = persisterFactory.createEntityPersister(model, accessStrategy, naturalIdAccessStrategy, persisterCreationContext);
        entityPersisterMap.put(model.getEntityName(), cp);
        if (cp.getConcreteProxyClass() != null && cp.getConcreteProxyClass().isInterface() && !Map.class.isAssignableFrom(cp.getConcreteProxyClass()) && cp.getMappedClass() != cp.getConcreteProxyClass()) {
            if (cp.getMappedClass().equals(cp.getConcreteProxyClass())) {
                // this part handles an odd case in the Hibernate test suite where we map an interface
                // as the class and the proxy.  I cannot think of a real life use case for that
                // specific test, but..
                log.debugf("Entity [%s] mapped same interface [%s] as class and proxy", cp.getEntityName(), cp.getMappedClass());
            } else {
                final String old = entityProxyInterfaceMap.put(cp.getConcreteProxyClass(), cp.getEntityName());
                if (old != null) {
                    throw new HibernateException(String.format(Locale.ENGLISH, "Multiple entities [%s, %s] named the same interface [%s] as their proxy which is not supported", old, cp.getEntityName(), cp.getConcreteProxyClass().getName()));
                }
            }
        }
    }
    for (final Collection model : mappingMetadata.getCollectionBindings()) {
        final CollectionRegionAccessStrategy accessStrategy = sessionFactory.getCache().determineCollectionRegionAccessStrategy(model);
        final CollectionPersister persister = persisterFactory.createCollectionPersister(model, accessStrategy, persisterCreationContext);
        collectionPersisterMap.put(model.getRole(), persister);
        Type indexType = persister.getIndexType();
        if (indexType != null && indexType.isAssociationType() && !indexType.isAnyType()) {
            String entityName = ((AssociationType) indexType).getAssociatedEntityName(sessionFactory);
            Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
            if (roles == null) {
                roles = new HashSet<>();
                collectionRolesByEntityParticipant.put(entityName, roles);
            }
            roles.add(persister.getRole());
        }
        Type elementType = persister.getElementType();
        if (elementType.isAssociationType() && !elementType.isAnyType()) {
            String entityName = ((AssociationType) elementType).getAssociatedEntityName(sessionFactory);
            Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
            if (roles == null) {
                roles = new HashSet<>();
                collectionRolesByEntityParticipant.put(entityName, roles);
            }
            roles.add(persister.getRole());
        }
    }
    // after *all* persisters and named queries are registered
    entityPersisterMap.values().forEach(EntityPersister::generateEntityDefinition);
    for (EntityPersister persister : entityPersisterMap.values()) {
        persister.postInstantiate();
        registerEntityNameResolvers(persister, entityNameResolvers);
    }
    collectionPersisterMap.values().forEach(CollectionPersister::postInstantiate);
    if (jpaMetaModelPopulationSetting != JpaMetaModelPopulationSetting.DISABLED) {
        MetadataContext context = new MetadataContext(sessionFactory, mappingMetadata.getMappedSuperclassMappingsCopy(), jpaMetaModelPopulationSetting);
        for (PersistentClass entityBinding : mappingMetadata.getEntityBindings()) {
            locateOrBuildEntityType(entityBinding, context);
        }
        handleUnusedMappedSuperclasses(context);
        context.wrapUp();
        this.jpaEntityTypeMap.putAll(context.getEntityTypeMap());
        this.jpaEmbeddableTypeMap.putAll(context.getEmbeddableTypeMap());
        this.jpaMappedSuperclassTypeMap.putAll(context.getMappedSuperclassTypeMap());
        this.jpaEntityTypesByEntityName.putAll(context.getEntityTypesByEntityName());
        applyNamedEntityGraphs(mappingMetadata.getNamedEntityGraphs().values());
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) HibernateException(org.hibernate.HibernateException) PersisterFactory(org.hibernate.persister.spi.PersisterFactory) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) NaturalIdRegionAccessStrategy(org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) PersisterCreationContext(org.hibernate.persister.spi.PersisterCreationContext) ManagedType(javax.persistence.metamodel.ManagedType) MappedSuperclassType(javax.persistence.metamodel.MappedSuperclassType) EntityType(javax.persistence.metamodel.EntityType) AssociationType(org.hibernate.type.AssociationType) EmbeddableType(javax.persistence.metamodel.EmbeddableType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssociationType(org.hibernate.type.AssociationType) Collection(org.hibernate.mapping.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)18 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5 Session (org.hibernate.Session)3 CollectionCacheEntry (org.hibernate.cache.spi.entry.CollectionCacheEntry)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 HibernateException (org.hibernate.HibernateException)2 CollectionRegion (org.hibernate.cache.spi.CollectionRegion)2 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 Type (org.hibernate.type.Type)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 EmbeddableType (javax.persistence.metamodel.EmbeddableType)1 EntityType (javax.persistence.metamodel.EntityType)1