Search in sources :

Example 1 with NavigableRole

use of org.hibernate.metamodel.model.domain.NavigableRole in project hibernate-orm by hibernate.

the class DomainDataRegionTemplate method generateNaturalIdAccess.

@Override
public NaturalIdDataAccess generateNaturalIdAccess(NaturalIdDataCachingConfig accessConfig) {
    final NavigableRole namedEntityRole = accessConfig.getNavigableRole();
    final AccessType accessType = accessConfig.getAccessType();
    log.debugf("Generating entity natural-id access [%s] : %s", accessType.getExternalName(), namedEntityRole);
    switch(accessType) {
        case READ_ONLY:
            {
                return generateReadOnlyNaturalIdAccess(accessConfig);
            }
        case READ_WRITE:
            {
                return generateReadWriteNaturalIdAccess(accessConfig);
            }
        case NONSTRICT_READ_WRITE:
            {
                return generateNonStrictReadWriteNaturalIdAccess(accessConfig);
            }
        case TRANSACTIONAL:
            {
                return generateTransactionalNaturalIdDataAccess(accessConfig);
            }
        default:
            {
                throw new IllegalArgumentException("Unrecognized cache AccessType - " + accessType);
            }
    }
}
Also used : NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) AccessType(org.hibernate.cache.spi.access.AccessType)

Example 2 with NavigableRole

use of org.hibernate.metamodel.model.domain.NavigableRole in project hibernate-orm by hibernate.

the class DomainDataRegionTemplate method generateEntityAccess.

@Override
public EntityDataAccess generateEntityAccess(EntityDataCachingConfig entityAccessConfig) {
    final NavigableRole namedEntityRole = entityAccessConfig.getNavigableRole();
    final AccessType accessType = entityAccessConfig.getAccessType();
    log.debugf("Generating entity cache access [%s] : %s", accessType.getExternalName(), namedEntityRole);
    switch(accessType) {
        case READ_ONLY:
            {
                return generateReadOnlyEntityAccess(entityAccessConfig);
            }
        case READ_WRITE:
            {
                return generateReadWriteEntityAccess(entityAccessConfig);
            }
        case NONSTRICT_READ_WRITE:
            {
                return generateNonStrictReadWriteEntityAccess(entityAccessConfig);
            }
        case TRANSACTIONAL:
            {
                return generateTransactionalEntityDataAccess(entityAccessConfig);
            }
        default:
            {
                throw new IllegalArgumentException("Unrecognized cache AccessType - " + accessType);
            }
    }
}
Also used : NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) AccessType(org.hibernate.cache.spi.access.AccessType)

Example 3 with NavigableRole

use of org.hibernate.metamodel.model.domain.NavigableRole in project hibernate-orm by hibernate.

the class RegionNameTest method testLegacyCacheSpi.

@Test
public void testLegacyCacheSpi() {
    // these need to be the prefixed name
    final String regionName = cachePrefix + '.' + localName;
    final CacheImplementor cache = sessionFactory().getCache();
    // just like stats, the cache for queries cannot be accessed second level cache regions map
    assertEquals(2, cache.getSecondLevelCacheRegionNames().length);
    boolean foundRegion = false;
    for (String name : cache.getSecondLevelCacheRegionNames()) {
        if (EqualsHelper.areEqual(name, regionName)) {
            foundRegion = true;
            break;
        }
    }
    if (!foundRegion) {
        fail("Could not find region [" + regionName + "] in reported list of region names");
    }
    final NavigableRole personEntityName = new NavigableRole(Person.class.getName());
    final NavigableRole nickNamesRole = personEntityName.append("nickNames");
    assert cache.getEntityRegionAccess(personEntityName) != null;
    assert cache.getNaturalIdCacheRegionAccessStrategy(personEntityName) != null;
    assert cache.getCollectionRegionAccess(nickNamesRole) != null;
}
Also used : NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) CacheImplementor(org.hibernate.engine.spi.CacheImplementor) Test(org.junit.Test)

Example 4 with NavigableRole

use of org.hibernate.metamodel.model.domain.NavigableRole in project hibernate-orm by hibernate.

the class DomainDataRegionTemplate method generateCollectionAccess.

@Override
public CollectionDataAccess generateCollectionAccess(CollectionDataCachingConfig accessConfig) {
    final NavigableRole namedCollectionRole = accessConfig.getNavigableRole();
    log.debugf("Generating collection cache access : %s", namedCollectionRole);
    switch(accessConfig.getAccessType()) {
        case READ_ONLY:
            {
                return generateReadOnlyCollectionAccess(accessConfig);
            }
        case READ_WRITE:
            {
                return generateReadWriteCollectionAccess(accessConfig);
            }
        case NONSTRICT_READ_WRITE:
            {
                return generateNonStrictReadWriteCollectionAccess(accessConfig);
            }
        case TRANSACTIONAL:
            {
                return generateTransactionalCollectionDataAccess(accessConfig);
            }
        default:
            {
                throw new IllegalArgumentException("Unrecognized cache AccessType - " + accessConfig.getAccessType());
            }
    }
}
Also used : NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole)

Example 5 with NavigableRole

use of org.hibernate.metamodel.model.domain.NavigableRole 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());
    primeSecondLevelCacheRegions(mappingMetadata);
    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 NavigableRole rootEntityRole = new NavigableRole(model.getRootClass().getEntityName());
        final EntityDataAccess accessStrategy = sessionFactory.getCache().getEntityRegionAccess(rootEntityRole);
        final NaturalIdDataAccess naturalIdAccessStrategy = sessionFactory.getCache().getNaturalIdCacheRegionAccessStrategy(rootEntityRole);
        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 NavigableRole navigableRole = new NavigableRole(model.getRole());
        final CollectionDataAccess accessStrategy = sessionFactory.getCache().getCollectionRegionAccess(navigableRole);
        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) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) PersisterCreationContext(org.hibernate.persister.spi.PersisterCreationContext) AccessType(org.hibernate.cache.spi.access.AccessType) 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) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) Collection(org.hibernate.mapping.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) PersistentClass(org.hibernate.mapping.PersistentClass) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Aggregations

NavigableRole (org.hibernate.metamodel.model.domain.NavigableRole)5 AccessType (org.hibernate.cache.spi.access.AccessType)3 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 ManagedType (javax.persistence.metamodel.ManagedType)1 MappedSuperclassType (javax.persistence.metamodel.MappedSuperclassType)1 HibernateException (org.hibernate.HibernateException)1 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)1 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)1 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)1 CacheImplementor (org.hibernate.engine.spi.CacheImplementor)1 Collection (org.hibernate.mapping.Collection)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 PersisterCreationContext (org.hibernate.persister.spi.PersisterCreationContext)1 PersisterFactory (org.hibernate.persister.spi.PersisterFactory)1