Search in sources :

Example 6 with StandardServiceRegistryImpl

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

the class ManagedProviderConnectionHelper method createServiceRegistry.

private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
    Environment.verifyProperties(properties);
    ConfigurationHelper.resolvePlaceHolders(properties);
    return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings(properties).build();
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)

Example 7 with StandardServiceRegistryImpl

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

the class ConnectionCreatorTest method testBadUrl.

@Test
@TestForIssue(jiraKey = "HHH-8621")
public void testBadUrl() throws Exception {
    DriverConnectionCreator connectionCreator = new DriverConnectionCreator((Driver) Class.forName("org.h2.Driver").newInstance(), new StandardServiceRegistryImpl(true, new BootstrapServiceRegistryImpl(), Collections.<StandardServiceInitiator>emptyList(), Collections.<ProvidedService>emptyList(), Collections.emptyMap()) {

        @Override
        @SuppressWarnings("unchecked")
        public <R extends Service> R getService(Class<R> serviceRole) {
            if (JdbcServices.class.equals(serviceRole)) {
                // return a new, not fully initialized JdbcServicesImpl
                return (R) new JdbcServicesImpl();
            }
            return super.getService(serviceRole);
        }
    }, "jdbc:h2:mem:test-bad-urls;nosuchparam=saywhat", new Properties(), false, null);
    try {
        Connection conn = connectionCreator.createConnection();
        conn.close();
        fail("Expecting the bad Connection URL to cause an exception");
    } catch (JDBCConnectionException expected) {
    }
}
Also used : JdbcServicesImpl(org.hibernate.engine.jdbc.internal.JdbcServicesImpl) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) ProvidedService(org.hibernate.service.internal.ProvidedService) DriverConnectionCreator(org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator) StandardServiceInitiator(org.hibernate.boot.registry.StandardServiceInitiator) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) Properties(java.util.Properties) BootstrapServiceRegistryImpl(org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 with StandardServiceRegistryImpl

use of org.hibernate.boot.registry.internal.StandardServiceRegistryImpl in project zhcet-web by zhcet-amu.

the class Hibernate5DDLExporter method schemaExport.

private Hibernate5DDLExporter schemaExport(String fileName, String targetDirectory) throws Exception {
    if (entityPackages == null && entityPackages.length == 0) {
        System.out.println("Not packages selected");
        System.exit(0);
    }
    File exportFile = createExportFileAndMakeDirectory(fileName, targetDirectory);
    PhysicalNamingStrategy physicalNamingStrategy;
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, dialect).applySetting(AvailableSettings.PHYSICAL_NAMING_STRATEGY, "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy").build();
    MetadataImplementor metadata = (MetadataImplementor) mapAnnotatedClasses(serviceRegistry).buildMetadata();
    SchemaExport schemaExport = new SchemaExport();
    schemaExport.setOutputFile(exportFile.getAbsolutePath());
    schemaExport.setDelimiter(";");
    schemaExport.setFormat(true);
    schemaExport.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata, serviceRegistry);
    ((StandardServiceRegistryImpl) serviceRegistry).destroy();
    System.out.println(exportFile.getAbsolutePath());
    return this;
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ServiceRegistry(org.hibernate.service.ServiceRegistry) File(java.io.File) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 9 with StandardServiceRegistryImpl

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

the class InterceptorTest method testConfiguredSessionInterceptorWithSessionFactory.

@Test
public void testConfiguredSessionInterceptorWithSessionFactory() {
    StandardServiceRegistryImpl standardRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
    SessionFactory sessionFactory = null;
    try {
        MetadataSources metadataSources = new MetadataSources(standardRegistry);
        for (Class annotatedClass : getAnnotatedClasses()) {
            metadataSources.addAnnotatedClass(annotatedClass);
        }
        Metadata metadata = metadataSources.getMetadataBuilder().build();
        SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
        sessionFactoryBuilder.applyStatelessInterceptor(LocalExceptionInterceptor.class);
        sessionFactory = sessionFactoryBuilder.build();
        final SessionFactory sessionFactoryInstance = sessionFactory;
        Supplier<SessionFactory> sessionFactorySupplier = () -> sessionFactoryInstance;
        Item i = new Item();
        i.setName("Laptop");
        try {
            doInHibernate(sessionFactorySupplier, session -> {
                session.persist(i);
                fail("No interceptor");
                return null;
            });
        } catch (IllegalStateException e) {
            assertEquals(LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage());
        }
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        standardRegistry.destroy();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Item(org.hibernate.jpa.test.Item) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test)

Example 10 with StandardServiceRegistryImpl

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

the class SchemaToolTransactionHandlingTest method testValidateInExistingJtaTransaction.

@Test
public void testValidateInExistingJtaTransaction() {
    // start a JTA transaction...
    try {
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    } catch (Exception e) {
        throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
    }
    // hold reference to Transaction object
    final Transaction jtaTransaction;
    try {
        jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
    } catch (SystemException e) {
        throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
    }
    final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
    // perform the test...
    try {
        final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
        final Metadata mappings = buildMappings(registry);
        // first make the schema exist...
        try {
            smt.getSchemaCreator(Collections.emptyMap()).doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
        } catch (Exception e) {
            throw new RuntimeException("Unable to create schema to validation tests", e);
        }
        try {
            smt.getSchemaValidator(Collections.emptyMap()).doValidation(mappings, ExecutionOptionsTestImpl.INSTANCE);
        } finally {
            try {
                smt.getSchemaDropper(Collections.emptyMap()).doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (Exception ignore) {
            // ignore
            }
        }
    } finally {
        try {
            jtaTransaction.commit();
            ((StandardServiceRegistryImpl) registry).destroy();
        } catch (Exception e) {
        // not much we can do...
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) Metadata(org.hibernate.boot.Metadata) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) SystemException(javax.transaction.SystemException) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

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