Search in sources :

Example 6 with RuntimeModelCreationContext

use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.

the class MappingMetamodelImpl method finishInitialization.

public void finishInitialization(MetadataImplementor bootModel, BootstrapContext bootstrapContext, SessionFactoryImplementor sessionFactory) {
    final RuntimeModelCreationContext runtimeModelCreationContext = new RuntimeModelCreationContext() {

        @Override
        public BootstrapContext getBootstrapContext() {
            return bootstrapContext;
        }

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

        @Override
        public MetadataImplementor getBootModel() {
            return bootModel;
        }

        @Override
        public MappingMetamodel getDomainModel() {
            return MappingMetamodelImpl.this;
        }
    };
    final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService(PersisterFactory.class);
    final JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting = determineJpaStaticMetaModelPopulationSetting(sessionFactory.getProperties());
    final JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting = determineJpaMetaModelPopulationSetting(sessionFactory.getProperties());
    bootModel.visitRegisteredComponents(Component::prepareForMappingModel);
    bootModel.getMappedSuperclassMappingsCopy().forEach(MappedSuperclass::prepareForMappingModel);
    bootModel.getEntityBindings().forEach(PersistentClass::prepareForMappingModel);
    processBootEntities(bootModel.getEntityBindings(), sessionFactory.getCache(), persisterFactory, runtimeModelCreationContext);
    processBootCollections(bootModel.getCollectionBindings(), sessionFactory.getCache(), persisterFactory, runtimeModelCreationContext);
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // after *all* persisters and named queries are registered
    MappingModelCreationProcess.process(entityPersisterMap, sessionFactory.getQueryEngine().getSqmFunctionRegistry(), runtimeModelCreationContext);
    for (EntityPersister persister : entityPersisterMap.values()) {
        persister.postInstantiate();
        registerEntityNameResolvers(persister, entityNameResolvers);
    }
    collectionPersisterMap.values().forEach(CollectionPersister::postInstantiate);
    ((JpaMetamodelImpl) this.jpaMetamodel).processJpa(bootModel, this, entityProxyInterfaceMap, jpaStaticMetaModelPopulationSetting, jpaMetaModelPopulationSetting, bootModel.getNamedEntityGraphs().values(), runtimeModelCreationContext);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) PersisterFactory(org.hibernate.persister.spi.PersisterFactory) MappedSuperclass(org.hibernate.mapping.MappedSuperclass) JpaStaticMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting) JpaStaticMetaModelPopulationSetting.determineJpaStaticMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting.determineJpaStaticMetaModelPopulationSetting) JpaMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting) JpaMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting) Component(org.hibernate.mapping.Component) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with RuntimeModelCreationContext

use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method buildPluralAttributeMapping.

@SuppressWarnings("rawtypes")
public static PluralAttributeMapping buildPluralAttributeMapping(String attrName, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, FetchMode fetchMode, MappingModelCreationProcess creationProcess) {
    final Collection bootValueMapping = (Collection) bootProperty.getValue();
    final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
    final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
    final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
    final Dialect dialect = sqlStringGenerationContext.getDialect();
    final MappingMetamodel domainModel = creationContext.getDomainModel();
    final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor(bootValueMapping.getRole());
    assert collectionDescriptor != null;
    final String tableExpression = ((Joinable) collectionDescriptor).getTableName();
    final String sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName(bootProperty.getName());
    final CollectionMappingType<?> collectionMappingType;
    final JavaTypeRegistry jtdRegistry = creationContext.getJavaTypeRegistry();
    final CollectionPart elementDescriptor = interpretElement(bootValueMapping, tableExpression, collectionDescriptor, sqlAliasStem, dialect, creationProcess);
    final CollectionPart indexDescriptor;
    CollectionIdentifierDescriptor identifierDescriptor = null;
    final CollectionSemantics<?, ?> collectionSemantics = collectionDescriptor.getCollectionSemantics();
    switch(collectionSemantics.getCollectionClassification()) {
        case ARRAY:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(Object[].class), StandardArraySemantics.INSTANCE);
                final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
                final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
                indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
                null, selectableMapping);
                break;
            }
        case BAG:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardBagSemantics.INSTANCE);
                indexDescriptor = null;
                break;
            }
        case ID_BAG:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardIdentifierBagSemantics.INSTANCE);
                indexDescriptor = null;
                assert collectionDescriptor instanceof SQLLoadableCollection;
                final SQLLoadableCollection loadableCollection = (SQLLoadableCollection) collectionDescriptor;
                final String identifierColumnName = loadableCollection.getIdentifierColumnName();
                assert identifierColumnName != null;
                identifierDescriptor = new CollectionIdentifierDescriptorImpl(collectionDescriptor, tableExpression, identifierColumnName, (BasicType) loadableCollection.getIdentifierType());
                break;
            }
        case LIST:
            {
                final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
                final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
                indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
                null, selectableMapping);
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(List.class), StandardListSemantics.INSTANCE);
                break;
            }
        case MAP:
        case ORDERED_MAP:
        case SORTED_MAP:
            {
                final Class<? extends java.util.Map> mapJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedMap.class : java.util.Map.class;
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(mapJavaType), collectionSemantics);
                final String mapKeyTableExpression;
                if (bootValueMapping instanceof Map && ((Map) bootValueMapping).getMapKeyPropertyName() != null) {
                    mapKeyTableExpression = getTableIdentifierExpression(((Map) bootValueMapping).getIndex().getTable(), creationProcess);
                } else {
                    mapKeyTableExpression = tableExpression;
                }
                indexDescriptor = interpretMapKey(bootValueMapping, collectionDescriptor, mapKeyTableExpression, sqlAliasStem, dialect, creationProcess);
                break;
            }
        case SET:
        case ORDERED_SET:
        case SORTED_SET:
            {
                final Class<? extends java.util.Set> setJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedSet.class : java.util.Set.class;
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(setJavaType), collectionSemantics);
                indexDescriptor = null;
                break;
            }
        default:
            {
                throw new MappingException("Unexpected CollectionClassification : " + collectionSemantics.getCollectionClassification());
            }
    }
    final StateArrayContributorMetadata contributorMetadata = new StateArrayContributorMetadata() {

        @Override
        public PropertyAccess getPropertyAccess() {
            return propertyAccess;
        }

        @Override
        public MutabilityPlan getMutabilityPlan() {
            return ImmutableMutabilityPlan.instance();
        }

        @Override
        public boolean isNullable() {
            return bootProperty.isOptional();
        }

        @Override
        public boolean isInsertable() {
            return bootProperty.isInsertable();
        }

        @Override
        public boolean isUpdatable() {
            return bootProperty.isUpdateable();
        }

        @Override
        public boolean isIncludedInDirtyChecking() {
            return false;
        }

        @Override
        public boolean isIncludedInOptimisticLocking() {
            return bootProperty.isOptimisticLocked();
        }

        @Override
        public CascadeStyle getCascadeStyle() {
            return cascadeStyle;
        }
    };
    final FetchStyle style = FetchOptionsHelper.determineFetchStyleByMetadata(fetchMode, collectionDescriptor.getCollectionType(), sessionFactory);
    final FetchTiming timing = FetchOptionsHelper.determineFetchTiming(style, collectionDescriptor.getCollectionType(), collectionDescriptor.isLazy(), collectionDescriptor.getRole(), sessionFactory);
    final PluralAttributeMappingImpl pluralAttributeMapping = new PluralAttributeMappingImpl(attrName, bootValueMapping, propertyAccess, entityMappingType -> contributorMetadata, collectionMappingType, stateArrayPosition, elementDescriptor, indexDescriptor, identifierDescriptor, timing, style, cascadeStyle, declaringType, collectionDescriptor);
    creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ")#finishInitialization", () -> {
        pluralAttributeMapping.finishInitialization(bootProperty, bootValueMapping, creationProcess);
        return true;
    });
    creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ") - key descriptor", () -> {
        interpretPluralAttributeMappingKeyDescriptor(pluralAttributeMapping, bootValueMapping, collectionDescriptor, declaringType, dialect, creationProcess);
        return true;
    });
    return pluralAttributeMapping;
}
Also used : SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) SortedSet(java.util.SortedSet) SortedSet(java.util.SortedSet) BasicValue(org.hibernate.mapping.BasicValue) MappingException(org.hibernate.MappingException) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) FetchStyle(org.hibernate.engine.FetchStyle) Dialect(org.hibernate.dialect.Dialect) List(java.util.List) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionIdentifierDescriptor(org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) Joinable(org.hibernate.persister.entity.Joinable) SortedMap(java.util.SortedMap) FetchTiming(org.hibernate.engine.FetchTiming) Collection(org.hibernate.mapping.Collection) IndexedCollection(org.hibernate.mapping.IndexedCollection) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PersistentClass(org.hibernate.mapping.PersistentClass) IndexedCollection(org.hibernate.mapping.IndexedCollection) SortedMap(java.util.SortedMap) Map(org.hibernate.mapping.Map)

Example 8 with RuntimeModelCreationContext

use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.

the class SqmMutationStrategyHelper method resolveStrategy.

/**
 * Standard resolution of SqmMutationStrategy to use for a given
 * entity hierarchy.
 */
public static SqmMultiTableMutationStrategy resolveStrategy(RootClass entityBootDescriptor, EntityMappingType rootEntityDescriptor, MappingModelCreationProcess creationProcess) {
    final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
    final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
    final SessionFactoryOptions options = sessionFactory.getSessionFactoryOptions();
    final SqmMultiTableMutationStrategy specifiedStrategy = options.getCustomSqmMultiTableMutationStrategy();
    if (specifiedStrategy != null) {
        return specifiedStrategy;
    }
    return sessionFactory.getServiceRegistry().getService(JdbcServices.class).getJdbcEnvironment().getDialect().getFallbackSqmMutationStrategy(rootEntityDescriptor, creationContext);
}
Also used : RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) SqmMultiTableMutationStrategy(org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Aggregations

RuntimeModelCreationContext (org.hibernate.metamodel.spi.RuntimeModelCreationContext)8 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 PersistentClass (org.hibernate.mapping.PersistentClass)5 SessionFactoryOptions (org.hibernate.boot.spi.SessionFactoryOptions)3 List (java.util.List)2 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 MappedSuperclass (org.hibernate.mapping.MappedSuperclass)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 JpaMetaModelPopulationSetting (org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting)2 JpaStaticMetaModelPopulationSetting (org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 EntityGraph (jakarta.persistence.EntityGraph)1 NamedAttributeNode (jakarta.persistence.NamedAttributeNode)1 NamedEntityGraph (jakarta.persistence.NamedEntityGraph)1 NamedSubgraph (jakarta.persistence.NamedSubgraph)1 Attribute (jakarta.persistence.metamodel.Attribute)1 EmbeddableType (jakarta.persistence.metamodel.EmbeddableType)1 EntityType (jakarta.persistence.metamodel.EntityType)1 ManagedType (jakarta.persistence.metamodel.ManagedType)1 ObjectStreamException (java.io.ObjectStreamException)1