Search in sources :

Example 6 with Database

use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.

the class SequenceStyleConfigUnitTest method testDefaultOptimizerBasedOnIncrementBackedByTable.

/**
	 * Test default optimizer selection for table backed generators
	 * based on the configured increment size.  Here we always prefer
	 * pooled.
	 */
@Test
public void testDefaultOptimizerBasedOnIncrementBackedByTable() {
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, TableDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "10");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 7 with Database

use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.

the class SequenceStyleConfigUnitTest method testDefaultOptimizerBasedOnIncrementBackedBySequence.

/**
	 * Test default optimizer selection for sequence backed generators
	 * based on the configured increment size; both in the case of the
	 * dialect supporting pooled sequences (pooled) and not (hilo)
	 */
@Test
public void testDefaultOptimizerBasedOnIncrementBackedBySequence() {
    // for dialects which do not support pooled sequences, we default to pooled+table
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, SequenceDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "10");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    // for dialects which do support pooled sequences, we default to pooled+sequence
    serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, PooledSequenceDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "10");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
        assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 8 with Database

use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.

the class SchemaUpdateTableBackedSequenceTest method testCreateTableOnUpdate.

@Test
public void testCreateTableOnUpdate() throws SQLException {
    Metadata metadata = new MetadataSources(ssr).buildMetadata();
    Database database = metadata.getDatabase();
    TableStructure tableStructure = new TableStructure(database.getJdbcEnvironment(), new QualifiedTableName(null, null, Identifier.toIdentifier("test_seq")), Identifier.toIdentifier("nextval"), 20, 30, Long.class);
    tableStructure.registerExportables(database);
    // lets make sure the InitCommand is there
    assertEquals(1, database.getDefaultNamespace().getTables().size());
    Table table = database.getDefaultNamespace().getTables().iterator().next();
    assertEquals(1, table.getInitCommands().size());
    final TargetImpl target = new TargetImpl();
    ssr.getService(SchemaManagementTool.class).getSchemaMigrator(Collections.emptyMap()).doMigration(metadata, new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return ssr.getService(ConfigurationService.class).getSettings();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    }, new TargetDescriptor() {

        @Override
        public EnumSet<TargetType> getTargetTypes() {
            return EnumSet.of(TargetType.SCRIPT, TargetType.DATABASE);
        }

        @Override
        public ScriptTargetOutput getScriptTargetOutput() {
            return target;
        }
    });
    assertTrue(target.found);
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) EnumSet(java.util.EnumSet) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) TableStructure(org.hibernate.id.enhanced.TableStructure) Database(org.hibernate.boot.model.relational.Database) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) Map(java.util.Map) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Example 9 with Database

use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.

the class CrossSchemaForeignKeyGenerationTest method testSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated.

@Test
@TestForIssue(jiraKey = "HHH-10420")
public void testSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(SchemaOneEntity.class);
    metadataSources.addAnnotatedClass(SchemaTwoEntity.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    final Database database = metadata.getDatabase();
    final HibernateSchemaManagementTool tool = (HibernateSchemaManagementTool) ssr.getService(SchemaManagementTool.class);
    final Map configurationValues = ssr.getService(ConfigurationService.class).getSettings();
    final ExecutionOptions options = new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return configurationValues;
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    };
    new IndividuallySchemaMigratorImpl(tool, DefaultSchemaFilter.INSTANCE).doMigration(metadata, options, TargetDescriptorImpl.INSTANCE);
    new SchemaDropperImpl(tool).doDrop(metadata, options, ssr.getService(JdbcEnvironment.class).getDialect(), new SourceDescriptor() {

        @Override
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        @Override
        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    }, buildTargets());
}
Also used : HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) SourceDescriptor(org.hibernate.tool.schema.spi.SourceDescriptor) ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) SourceType(org.hibernate.tool.schema.SourceType) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) Database(org.hibernate.boot.model.relational.Database) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Map(java.util.Map) IndividuallySchemaMigratorImpl(org.hibernate.tool.schema.internal.IndividuallySchemaMigratorImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with Database

use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.

the class SequenceStyleConfigUnitTest method testPreferredPooledOptimizerSetting.

@Test
public void testPreferredPooledOptimizerSetting() {
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, PooledSequenceDialect.class.getName()).build();
    try {
        Properties props = buildGeneratorPropertiesBase(serviceRegistry);
        props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
        SequenceStyleGenerator generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
        props.setProperty(Environment.PREFER_POOLED_VALUES_LO, "true");
        generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledLoOptimizer.class, generator.getOptimizer().getClass());
        props.setProperty(Environment.PREFERRED_POOLED_OPTIMIZER, StandardOptimizerDescriptor.POOLED_LOTL.getExternalName());
        generator = new SequenceStyleGenerator();
        generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
        generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
        assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
        assertClassAssignability(PooledLoThreadLocalOptimizer.class, generator.getOptimizer().getClass());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Database(org.hibernate.boot.model.relational.Database) Properties(java.util.Properties) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

Database (org.hibernate.boot.model.relational.Database)18 Test (org.junit.Test)9 Properties (java.util.Properties)7 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)7 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)7 Identifier (org.hibernate.boot.model.naming.Identifier)6 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)4 Table (org.hibernate.mapping.Table)4 HashSet (java.util.HashSet)3 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)3 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)3 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)3 Namespace (org.hibernate.boot.model.relational.Namespace)3 Sequence (org.hibernate.boot.model.relational.Sequence)3 Map (java.util.Map)2 AnnotationException (org.hibernate.AnnotationException)2 MetadataSources (org.hibernate.boot.MetadataSources)2 ImplicitBasicColumnNameSource (org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource)2 AttributePath (org.hibernate.boot.model.source.spi.AttributePath)2 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)2