Search in sources :

Example 6 with DdlTransactionIsolatorTestingImpl

use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.

the class SuppliedConnectionTest method prepareTest.

@Override
protected void prepareTest() throws Exception {
    super.prepareTest();
    try {
        Connection conn = cp.getConnection();
        try {
            final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry(), conn), true);
            new SchemaCreatorImpl(serviceRegistry()).doCreation(metadata(), false, target);
        } finally {
            cp.closeConnection(conn);
        }
    } catch (Throwable ignore) {
    }
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) Connection(java.sql.Connection) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)

Example 7 with DdlTransactionIsolatorTestingImpl

use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.

the class DiscriminatorMultiTenancyTest method setUp.

@Before
public void setUp() {
    Map<String, Object> settings = new HashMap<>();
    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();
    try {
        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();
        currentTenantResolver.setHibernateBooted();
    } catch (Throwable t) {
        serviceRegistry.close();
        throw t;
    }
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) Customer(org.hibernate.orm.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.orm.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) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 8 with DdlTransactionIsolatorTestingImpl

use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.

the class AbstractSchemaBasedMultiTenancyTest method setUp.

@Before
public void setUp() {
    T multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
    Map<String, Object> settings = new HashMap<>();
    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.orm.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) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 9 with DdlTransactionIsolatorTestingImpl

use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.

the class PostgreSQLMultipleSchemaSequenceTest method test.

@Test
@TestForIssue(jiraKey = "HHH-5538")
public void test() {
    StandardServiceRegistry ssr1 = new StandardServiceRegistryBuilder().build();
    final String extraSchemaName = "extra_schema_sequence_validation";
    try {
        final MetadataImplementor metadata1 = (MetadataImplementor) new MetadataSources(ssr1).addAnnotatedClass(Box.class).buildMetadata();
        try {
            new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata1);
            final ConnectionProvider connectionProvider1 = ssr1.getService(ConnectionProvider.class);
            DdlTransactionIsolatorTestingImpl ddlTransactionIsolator1 = new DdlTransactionIsolatorTestingImpl(ssr1, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider1));
            try (Statement statement = ddlTransactionIsolator1.getIsolatedConnection().createStatement()) {
                statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                statement.execute(String.format("CREATE SCHEMA %s;", extraSchemaName));
                try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                    while (resultSet.next()) {
                        Long sequenceValue = resultSet.getLong(1);
                        assertEquals(Long.valueOf(1L), sequenceValue);
                    }
                }
            } catch (SQLException e) {
                fail(e.getMessage());
            }
            String existingUrl = (String) Environment.getProperties().get(AvailableSettings.URL);
            if (existingUrl.indexOf('?') == -1) {
                existingUrl += "?";
            } else {
                existingUrl += "&";
            }
            StandardServiceRegistry ssr2 = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.URL, existingUrl + "currentSchema=" + extraSchemaName).build();
            try {
                final MetadataImplementor metadata2 = (MetadataImplementor) new MetadataSources(ssr2).addAnnotatedClass(Box.class).buildMetadata();
                try {
                    new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata2);
                } finally {
                    final ConnectionProvider connectionProvider2 = ssr2.getService(ConnectionProvider.class);
                    DdlTransactionIsolatorTestingImpl ddlTransactionIsolator2 = new DdlTransactionIsolatorTestingImpl(ssr2, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider2));
                    try (Statement statement = ddlTransactionIsolator2.getIsolatedConnection().createStatement()) {
                        try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                            while (resultSet.next()) {
                                Long sequenceValue = resultSet.getLong(1);
                                assertEquals(Long.valueOf(1L), sequenceValue);
                            }
                        }
                        statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                    } catch (SQLException e) {
                        fail(e.getMessage());
                    }
                    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata2);
                }
            } finally {
                StandardServiceRegistryBuilder.destroy(ssr2);
            }
        } finally {
            // clean up
            new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata1);
        }
        final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
        assertEquals(2, sqlLines.stream().filter(s -> s.equalsIgnoreCase("create sequence SEQ_TEST start with 1 increment by 1;")).count());
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr1);
    }
}
Also used : JdbcEnvironmentInitiator(org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) Statement(java.sql.Statement) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IOException(java.io.IOException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DdlTransactionIsolatorTestingImpl(org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl) ResultSet(java.sql.ResultSet) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with DdlTransactionIsolatorTestingImpl

use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.

the class IndividuallySchemaValidatorImplTest method testMissingColumn.

@Test
public void testMissingColumn() {
    MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(NoNameColumn.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    Map<String, Object> settings = new HashMap<>();
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
    connectionProvider.configure(PropertiesHelper.map(properties()));
    final GenerationTargetToDatabase schemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, new JdbcConnectionAccessImpl(connectionProvider)));
    try {
        new SchemaCreatorImpl(ssr).doCreation(metadata, serviceRegistry, settings, true, schemaGenerator);
        metadataSources = new MetadataSources(ssr);
        metadataSources.addAnnotatedClass(NameColumn.class);
        metadata = (MetadataImplementor) metadataSources.buildMetadata();
        metadata.validate();
        try {
            getSchemaValidator(metadata);
            Assert.fail("SchemaManagementException expected");
        } catch (SchemaManagementException e) {
            assertEquals("Schema-validation: missing column [name] in table [SomeSchema.ColumnEntity]", e.getMessage());
        }
    } finally {
        new SchemaDropperImpl(serviceRegistry).doDrop(metadata, false, schemaGenerator);
        serviceRegistry.destroy();
        connectionProvider.stop();
    }
}
Also used : JdbcConnectionAccessImpl(org.hibernate.testing.boot.JdbcConnectionAccessImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DdlTransactionIsolatorTestingImpl(org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) Test(org.junit.Test)

Aggregations

DdlTransactionIsolatorTestingImpl (org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl)11 GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)10 MetadataSources (org.hibernate.boot.MetadataSources)9 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)9 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)9 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)8 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)6 HashMap (java.util.HashMap)5 Metadata (org.hibernate.boot.Metadata)5 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)4 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)4 Test (org.junit.Test)4 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)3 JdbcConnectionAccessImpl (org.hibernate.testing.boot.JdbcConnectionAccessImpl)3 Connection (java.sql.Connection)2 ConnectionProvider (org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)2 JdbcEnvironmentInitiator (org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 RootClass (org.hibernate.mapping.RootClass)2