Search in sources :

Example 1 with HibernateSchemaManagementTool

use of org.hibernate.tool.schema.internal.HibernateSchemaManagementTool in project hibernate-orm by hibernate.

the class SchemaBasedMultiTenancyTest method setUp.

@Before
public void setUp() {
    AbstractMultiTenantConnectionProvider multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).addService(MultiTenantConnectionProvider.class, multiTenantConnectionProvider).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    ms.addAnnotatedClass(Invoice.class);
    Metadata metadata = ms.buildMetadata();
    ((RootClass) metadata.getEntityBinding(Customer.class.getName())).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase acmeTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, acmeProvider));
    final GenerationTargetToDatabase jbossTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, jbossProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    configure(sfb);
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) AbstractMultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 2 with HibernateSchemaManagementTool

use of org.hibernate.tool.schema.internal.HibernateSchemaManagementTool in project hibernate-orm by hibernate.

the class CrossSchemaForeignKeyGenerationTest method testImprovedSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated.

@Test
@TestForIssue(jiraKey = "HHH-10420")
public void testImprovedSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(SchemaOneEntity.class);
    metadataSources.addAnnotatedClass(SchemaTwoEntity.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    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 GroupedSchemaMigratorImpl(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) GroupedSchemaMigratorImpl(org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Map(java.util.Map) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with HibernateSchemaManagementTool

use of org.hibernate.tool.schema.internal.HibernateSchemaManagementTool in project hibernate-orm by hibernate.

the class DiscriminatorMultiTenancyTest method setUp.

@Before
public void setUp() {
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DISCRIMINATOR);
    settings.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantResolver);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    Metadata metadata = ms.buildMetadata();
    final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
    customerMapping.setCached(true);
    ((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
    final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, target);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, target);
    target.release();
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) Customer(org.hibernate.test.multitenancy.schema.Customer) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 4 with HibernateSchemaManagementTool

use of org.hibernate.tool.schema.internal.HibernateSchemaManagementTool in project hibernate-orm by hibernate.

the class AbstractSchemaBasedMultiTenancyTest method setUp.

@Before
public void setUp() {
    T multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).addService(MultiTenantConnectionProvider.class, multiTenantConnectionProvider).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    ms.addAnnotatedClass(Invoice.class);
    Metadata metadata = ms.buildMetadata();
    final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
    customerMapping.setCached(true);
    ((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase acmeTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, acmeProvider));
    final GenerationTargetToDatabase jbossTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, jbossProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    configure(sfb);
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 5 with HibernateSchemaManagementTool

use of org.hibernate.tool.schema.internal.HibernateSchemaManagementTool in project hibernate-orm by hibernate.

the class AbstractMultiTenancyTest method sessionFactory.

protected SessionFactory sessionFactory(Map<String, Object> settings) {
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    for (Class annotatedClasses : getAnnotatedClasses()) {
        metadataSources.addAnnotatedClass(annotatedClasses);
    }
    Metadata metadata = metadataSources.buildMetadata();
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase frontEndSchemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProviderMap.get(FRONT_END_TENANT)));
    final GenerationTargetToDatabase backEndSchemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProviderMap.get(BACK_END_TENANT)));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, frontEndSchemaGenerator, backEndSchemaGenerator);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, frontEndSchemaGenerator, backEndSchemaGenerator);
    final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
    return sessionFactoryBuilder.build();
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)

Aggregations

MetadataSources (org.hibernate.boot.MetadataSources)6 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)6 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)6 Map (java.util.Map)5 Metadata (org.hibernate.boot.Metadata)4 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)4 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)4 GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)4 HashMap (java.util.HashMap)3 RootClass (org.hibernate.mapping.RootClass)3 CachingRegionFactory (org.hibernate.testing.cache.CachingRegionFactory)3 Before (org.junit.Before)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)2 MultiTenantConnectionProvider (org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)2 TestForIssue (org.hibernate.testing.TestForIssue)2