Search in sources :

Example 1 with Internal

use of org.hibernate.Internal in project hibernate-orm by hibernate.

the class SingleIdEntityLoaderStandardImpl method resolveLoadPlan.

@Internal
public SingleIdLoadPlan<T> resolveLoadPlan(LockOptions lockOptions, LoadQueryInfluencers loadQueryInfluencers, SessionFactoryImplementor sessionFactory) {
    if (getLoadable().isAffectedByEnabledFilters(loadQueryInfluencers)) {
        // special case of not-cacheable based on enabled filters effecting this load.
        // 
        // This case is special because the filters need to be applied in order to
        // properly restrict the SQL/JDBC results.  For this reason it has higher
        // precedence than even "internal" fetch profiles.
        nonReusablePlansGenerated.incrementAndGet();
        return createLoadPlan(lockOptions, loadQueryInfluencers, sessionFactory);
    }
    final CascadingFetchProfile enabledCascadingFetchProfile = loadQueryInfluencers.getEnabledCascadingFetchProfile();
    if (enabledCascadingFetchProfile != null) {
        if (LockMode.UPGRADE.greaterThan(lockOptions.getLockMode())) {
            if (selectByInternalCascadeProfile == null) {
                selectByInternalCascadeProfile = new EnumMap<>(CascadingFetchProfile.class);
            } else {
                final SingleIdLoadPlan existing = selectByInternalCascadeProfile.get(enabledCascadingFetchProfile);
                if (existing != null) {
                    // noinspection unchecked
                    return existing;
                }
            }
            final SingleIdLoadPlan<T> plan = createLoadPlan(lockOptions, loadQueryInfluencers, sessionFactory);
            selectByInternalCascadeProfile.put(enabledCascadingFetchProfile, plan);
            return plan;
        }
    }
    // otherwise see if the loader for the requested load can be cached - which
    // also means we should look in the cache for an existing one
    final boolean reusable = determineIfReusable(lockOptions, loadQueryInfluencers);
    if (reusable) {
        final SingleIdLoadPlan existing = selectByLockMode.get(lockOptions.getLockMode());
        if (existing != null) {
            // noinspection unchecked
            return existing;
        }
        final SingleIdLoadPlan<T> plan = createLoadPlan(lockOptions, loadQueryInfluencers, sessionFactory);
        selectByLockMode.put(lockOptions.getLockMode(), plan);
        return plan;
    }
    nonReusablePlansGenerated.incrementAndGet();
    return createLoadPlan(lockOptions, loadQueryInfluencers, sessionFactory);
}
Also used : CascadingFetchProfile(org.hibernate.loader.ast.spi.CascadingFetchProfile) Internal(org.hibernate.Internal)

Example 2 with Internal

use of org.hibernate.Internal in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method createFromMetadata.

@Internal
public void createFromMetadata(Metadata metadata, ExecutionOptions options, ContributableMatcher contributableInclusionMatcher, Dialect dialect, Formatter formatter, GenerationTarget... targets) {
    boolean tryToCreateCatalogs = false;
    boolean tryToCreateSchemas = false;
    if (options.shouldManageNamespaces()) {
        if (dialect.canCreateSchema()) {
            tryToCreateSchemas = true;
        }
        if (dialect.canCreateCatalog()) {
            tryToCreateCatalogs = true;
        }
    }
    final Database database = metadata.getDatabase();
    final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
    SqlStringGenerationContext sqlStringGenerationContext = SqlStringGenerationContextImpl.fromConfigurationMap(jdbcEnvironment, database, options.getConfigurationValues());
    final Set<String> exportIdentifiers = CollectionHelper.setOfSize(50);
    // first, create each catalog/schema
    if (tryToCreateCatalogs || tryToCreateSchemas) {
        Set<Identifier> exportedCatalogs = new HashSet<>();
        for (Namespace namespace : database.getNamespaces()) {
            if (!options.getSchemaFilter().includeNamespace(namespace)) {
                continue;
            }
            if (tryToCreateCatalogs) {
                final Identifier catalogLogicalName = namespace.getName().getCatalog();
                final Identifier catalogPhysicalName = sqlStringGenerationContext.catalogWithDefault(namespace.getPhysicalName().getCatalog());
                if (catalogPhysicalName != null && !exportedCatalogs.contains(catalogLogicalName)) {
                    applySqlStrings(dialect.getCreateCatalogCommand(catalogPhysicalName.render(dialect)), formatter, options, targets);
                    exportedCatalogs.add(catalogLogicalName);
                }
            }
            final Identifier schemaPhysicalName = sqlStringGenerationContext.schemaWithDefault(namespace.getPhysicalName().getSchema());
            if (tryToCreateSchemas && schemaPhysicalName != null) {
                applySqlStrings(dialect.getCreateSchemaCommand(schemaPhysicalName.render(dialect)), formatter, options, targets);
            }
        }
    }
    // next, create all "before table" auxiliary objects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (!auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            continue;
        }
        if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
            checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
            applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata, sqlStringGenerationContext), formatter, options, targets);
        }
    }
    // then, create all schema objects (tables, sequences, constraints, etc) in each schema
    for (Namespace namespace : database.getNamespaces()) {
        if (!options.getSchemaFilter().includeNamespace(namespace)) {
            continue;
        }
        // sequences
        for (Sequence sequence : namespace.getSequences()) {
            if (!options.getSchemaFilter().includeSequence(sequence)) {
                continue;
            }
            if (!contributableInclusionMatcher.matches(sequence)) {
                continue;
            }
            checkExportIdentifier(sequence, exportIdentifiers);
            applySqlStrings(dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata, sqlStringGenerationContext), // ),
            formatter, options, targets);
        }
        // tables
        for (Table table : namespace.getTables()) {
            if (!table.isPhysicalTable()) {
                continue;
            }
            if (!options.getSchemaFilter().includeTable(table)) {
                continue;
            }
            if (!contributableInclusionMatcher.matches(table)) {
                continue;
            }
            checkExportIdentifier(table, exportIdentifiers);
            applySqlStrings(dialect.getTableExporter().getSqlCreateStrings(table, metadata, sqlStringGenerationContext), formatter, options, targets);
        }
        for (Table table : namespace.getTables()) {
            if (!table.isPhysicalTable()) {
                continue;
            }
            if (!options.getSchemaFilter().includeTable(table)) {
                continue;
            }
            if (!contributableInclusionMatcher.matches(table)) {
                continue;
            }
            // indexes
            final Iterator<Index> indexItr = table.getIndexIterator();
            while (indexItr.hasNext()) {
                final Index index = indexItr.next();
                checkExportIdentifier(index, exportIdentifiers);
                applySqlStrings(dialect.getIndexExporter().getSqlCreateStrings(index, metadata, sqlStringGenerationContext), formatter, options, targets);
            }
            // unique keys
            for (UniqueKey uniqueKey : table.getUniqueKeys().values()) {
                checkExportIdentifier(uniqueKey, exportIdentifiers);
                applySqlStrings(dialect.getUniqueKeyExporter().getSqlCreateStrings(uniqueKey, metadata, sqlStringGenerationContext), formatter, options, targets);
            }
        }
    }
    // NOTE : Foreign keys must be created *after* all tables of all namespaces for cross namespace fks. see HHH-10420
    for (Namespace namespace : database.getNamespaces()) {
        if (!options.getSchemaFilter().includeNamespace(namespace)) {
            continue;
        }
        for (Table table : namespace.getTables()) {
            if (!options.getSchemaFilter().includeTable(table)) {
                continue;
            }
            if (!contributableInclusionMatcher.matches(table)) {
                continue;
            }
            // foreign keys
            for (ForeignKey foreignKey : table.getForeignKeys().values()) {
                applySqlStrings(dialect.getForeignKeyExporter().getSqlCreateStrings(foreignKey, metadata, sqlStringGenerationContext), formatter, options, targets);
            }
        }
    }
    // next, create all "after table" auxiliary objects
    for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
        if (auxiliaryDatabaseObject.appliesToDialect(dialect) && !auxiliaryDatabaseObject.beforeTablesOnCreation()) {
            checkExportIdentifier(auxiliaryDatabaseObject, exportIdentifiers);
            applySqlStrings(dialect.getAuxiliaryDatabaseObjectExporter().getSqlCreateStrings(auxiliaryDatabaseObject, metadata, sqlStringGenerationContext), formatter, options, targets);
        }
    }
    // and finally add all init commands
    for (InitCommand initCommand : database.getInitCommands()) {
        // todo: this should alo probably use the DML formatter...
        applySqlStrings(initCommand.getInitCommands(), formatter, options, targets);
    }
}
Also used : SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) Table(org.hibernate.mapping.Table) InitCommand(org.hibernate.boot.model.relational.InitCommand) Index(org.hibernate.mapping.Index) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Sequence(org.hibernate.boot.model.relational.Sequence) ForeignKey(org.hibernate.mapping.ForeignKey) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) Namespace(org.hibernate.boot.model.relational.Namespace) Identifier(org.hibernate.boot.model.naming.Identifier) UniqueKey(org.hibernate.mapping.UniqueKey) Database(org.hibernate.boot.model.relational.Database) HashSet(java.util.HashSet) Internal(org.hibernate.Internal)

Example 3 with Internal

use of org.hibernate.Internal in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method doCreation.

/**
 * Intended for use from tests
 */
@Internal
public void doCreation(Metadata metadata, final boolean manageNamespaces, GenerationTarget... targets) {
    final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
    doCreation(metadata, serviceRegistry, serviceRegistry.getService(ConfigurationService.class).getSettings(), manageNamespaces, targets);
}
Also used : ServiceRegistry(org.hibernate.service.ServiceRegistry) Internal(org.hibernate.Internal)

Aggregations

Internal (org.hibernate.Internal)3 HashSet (java.util.HashSet)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 Database (org.hibernate.boot.model.relational.Database)1 InitCommand (org.hibernate.boot.model.relational.InitCommand)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 Sequence (org.hibernate.boot.model.relational.Sequence)1 SqlStringGenerationContext (org.hibernate.boot.model.relational.SqlStringGenerationContext)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 CascadingFetchProfile (org.hibernate.loader.ast.spi.CascadingFetchProfile)1 ForeignKey (org.hibernate.mapping.ForeignKey)1 Index (org.hibernate.mapping.Index)1 Table (org.hibernate.mapping.Table)1 UniqueKey (org.hibernate.mapping.UniqueKey)1 ServiceRegistry (org.hibernate.service.ServiceRegistry)1