Search in sources :

Example 11 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class DeepCollectionElementTest method testInitialization.

@Test
public void testInitialization() throws Exception {
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass(A.class);
    configuration.addAnnotatedClass(B.class);
    configuration.addAnnotatedClass(C.class);
    StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(configuration.getProperties());
    try {
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
    } finally {
        serviceRegistry.destroy();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test)

Example 12 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class SchemaExportTask method doExecution.

private void doExecution() throws Exception {
    final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    final MetadataSources metadataSources = new MetadataSources(bsr);
    if (configurationFile != null) {
        ssrBuilder.configure(configurationFile);
    }
    if (propertiesFile != null) {
        ssrBuilder.loadProperties(propertiesFile);
    }
    ssrBuilder.applySettings(getProject().getProperties());
    for (String fileName : getFiles()) {
        if (fileName.endsWith(".jar")) {
            metadataSources.addJar(new File(fileName));
        } else {
            metadataSources.addFile(fileName);
        }
    }
    ssrBuilder.applySetting(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
    ExportType exportType = ExportType.interpret(drop, create);
    Target output = Target.interpret(!quiet, !text);
    if (output.doScript()) {
        ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_ACTION, exportType.getAction());
        final Object scriptTarget;
        if (outputFile == null) {
            scriptTarget = new OutputStreamWriter(System.out);
        } else {
            scriptTarget = outputFile;
        }
        if (exportType.doCreate()) {
            ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, scriptTarget);
        }
        if (exportType.doDrop()) {
            ssrBuilder.applySetting(AvailableSettings.HBM2DDL_SCRIPTS_DROP_TARGET, scriptTarget);
        }
    }
    if (output.doExport()) {
        ssrBuilder.applySetting(AvailableSettings.HBM2DDL_DATABASE_ACTION, exportType.getAction());
    }
    final StandardServiceRegistryImpl ssr = (StandardServiceRegistryImpl) ssrBuilder.build();
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(ssr);
    ClassLoaderService classLoaderService = bsr.getService(ClassLoaderService.class);
    if (implicitNamingStrategy != null) {
        metadataBuilder.applyImplicitNamingStrategy((ImplicitNamingStrategy) classLoaderService.classForName(implicitNamingStrategy).newInstance());
    }
    if (physicalNamingStrategy != null) {
        metadataBuilder.applyPhysicalNamingStrategy((PhysicalNamingStrategy) classLoaderService.classForName(physicalNamingStrategy).newInstance());
    }
    final MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();
    metadata.validate();
    SchemaManagementToolCoordinator.process(metadata, ssr, ssr.getService(ConfigurationService.class).getSettings(), DelayedDropRegistryNotAvailableImpl.INSTANCE);
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 13 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class ServiceBootstrappingTest method testBuildWithServiceOverride.

@Test
public void testBuildWithServiceOverride() {
    StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(ConnectionProviderBuilder.getConnectionProviderProperties()).build();
    Properties props = ConnectionProviderBuilder.getConnectionProviderProperties();
    props.setProperty(Environment.DIALECT, H2Dialect.class.getName());
    try {
        JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
        assertTrue(jdbcServices.getDialect() instanceof H2Dialect);
        ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
        assertTrue(connectionAccess.getConnectionProvider().isUnwrappableAs(DriverManagerConnectionProviderImpl.class));
    } finally {
        serviceRegistry.destroy();
    }
    try {
        serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(props).addService(ConnectionProvider.class, new UserSuppliedConnectionProviderImpl()).build();
        JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
        assertTrue(jdbcServices.getDialect() instanceof H2Dialect);
        ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(ConnectionProviderJdbcConnectionAccess.class, jdbcServices.getBootstrapJdbcConnectionAccess());
        assertTrue(connectionAccess.getConnectionProvider().isUnwrappableAs(UserSuppliedConnectionProviderImpl.class));
    } finally {
        serviceRegistry.destroy();
    }
}
Also used : DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) UserSuppliedConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) H2Dialect(org.hibernate.dialect.H2Dialect) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) Properties(java.util.Properties) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) ConnectionProviderJdbcConnectionAccess(org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Test(org.junit.Test)

Example 14 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class StandardServiceRegistryBuilder method build.

/**
 * Build the StandardServiceRegistry.
 *
 * @return The StandardServiceRegistry.
 */
@SuppressWarnings("unchecked")
public StandardServiceRegistry build() {
    applyServiceContributingIntegrators();
    applyServiceContributors();
    final Map settingsCopy = new HashMap();
    settingsCopy.putAll(settings);
    settingsCopy.put(org.hibernate.boot.cfgxml.spi.CfgXmlAccessService.LOADED_CONFIG_KEY, aggregatedCfgXml);
    Environment.verifyProperties(settingsCopy);
    ConfigurationHelper.resolvePlaceHolders(settingsCopy);
    return new StandardServiceRegistryImpl(autoCloseRegistry, bootstrapServiceRegistry, initiators, providedServices, settingsCopy);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)

Example 15 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project hibernate-orm by hibernate.

the class SQLServerDialectCollationTest method buildSessionFactory.

protected void buildSessionFactory() {
    BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
    StandardServiceRegistryImpl _serviceRegistry = buildServiceRegistry(bootRegistry, constructConfiguration());
    try {
        try (Connection connection = _serviceRegistry.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess().obtainConnection();
            Statement statement = connection.createStatement()) {
            connection.setAutoCommit(true);
            statement.executeUpdate("DROP DATABASE hibernate_orm_test_collation");
        } catch (SQLException e) {
            log.debug(e.getMessage());
        }
        try (Connection connection = _serviceRegistry.getService(JdbcServices.class).getBootstrapJdbcConnectionAccess().obtainConnection();
            Statement statement = connection.createStatement()) {
            connection.setAutoCommit(true);
            statement.executeUpdate("CREATE DATABASE hibernate_orm_test_collation COLLATE Latin1_General_CS_AS");
            statement.executeUpdate("ALTER DATABASE [hibernate_orm_test_collation] SET AUTO_CLOSE OFF ");
        } catch (SQLException e) {
            log.debug(e.getMessage());
        }
    } finally {
        _serviceRegistry.destroy();
    }
    super.buildSessionFactory();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)

Aggregations

StandardServiceRegistryImpl (org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)16 Test (org.junit.Test)10 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 Metadata (org.hibernate.boot.Metadata)4 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)4 Properties (java.util.Properties)3 SessionFactory (org.hibernate.SessionFactory)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 H2Dialect (org.hibernate.dialect.H2Dialect)3 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)3 ConnectionProviderJdbcConnectionAccess (org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess)3 File (java.io.File)2 Connection (java.sql.Connection)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 SystemException (javax.transaction.SystemException)2 Transaction (javax.transaction.Transaction)2 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)2 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 Configuration (org.hibernate.cfg.Configuration)2