Search in sources :

Example 11 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class DefaultMergeEventListener method createEntityCopyObserver.

private EntityCopyObserver createEntityCopyObserver(SessionFactoryImplementor sessionFactory) {
    final ServiceRegistry serviceRegistry = sessionFactory.getServiceRegistry();
    if (entityCopyObserverStrategy == null) {
        final ConfigurationService configurationService = serviceRegistry.getService(ConfigurationService.class);
        entityCopyObserverStrategy = configurationService.getSetting(AvailableSettings.MERGE_ENTITY_COPY_OBSERVER, new ConfigurationService.Converter<String>() {

            @Override
            public String convert(Object value) {
                return value.toString();
            }
        }, EntityCopyNotAllowedObserver.SHORT_NAME);
        LOG.debugf("EntityCopyObserver strategy: %s", entityCopyObserverStrategy);
    }
    final StrategySelector strategySelector = serviceRegistry.getService(StrategySelector.class);
    return strategySelector.resolveStrategy(EntityCopyObserver.class, entityCopyObserverStrategy);
}
Also used : ServiceRegistry(org.hibernate.service.ServiceRegistry) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector)

Example 12 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method generateCreationCommands.

/**
	 * For testing...
	 *
	 * @param metadata The metadata for which to generate the creation commands.
	 *
	 * @return The generation commands
	 */
public List<String> generateCreationCommands(Metadata metadata, final boolean manageNamespaces) {
    final JournalingGenerationTarget target = new JournalingGenerationTarget();
    final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
    final Dialect dialect = serviceRegistry.getService(JdbcEnvironment.class).getDialect();
    final ExecutionOptions options = new ExecutionOptions() {

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

        @Override
        public Map getConfigurationValues() {
            return Collections.emptyMap();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerHaltImpl.INSTANCE;
        }
    };
    createFromMetadata(metadata, options, dialect, FormatStyle.NONE.getFormatter(), target);
    return target.commands;
}
Also used : ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) Dialect(org.hibernate.dialect.Dialect) ServiceRegistry(org.hibernate.service.ServiceRegistry) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 13 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class SchemaDropperImpl method doDrop.

/**
	 * For tests
	 */
public void doDrop(Metadata metadata, boolean manageNamespaces, GenerationTarget... targets) {
    final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
    doDrop(metadata, serviceRegistry, serviceRegistry.getService(ConfigurationService.class).getSettings(), manageNamespaces, targets);
}
Also used : ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 14 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class JdbcCoordinatorTest method testConnectionClose.

@Test
public void testConnectionClose() throws NoSuchFieldException, IllegalAccessException, SQLException {
    Connection connection = Mockito.mock(Connection.class);
    JdbcSessionOwner sessionOwner = Mockito.mock(JdbcSessionOwner.class);
    JdbcConnectionAccess jdbcConnectionAccess = Mockito.mock(JdbcConnectionAccess.class);
    when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
    when(jdbcConnectionAccess.supportsAggressiveRelease()).thenReturn(false);
    JdbcSessionContext sessionContext = Mockito.mock(JdbcSessionContext.class);
    when(sessionOwner.getJdbcSessionContext()).thenReturn(sessionContext);
    when(sessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
    ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
    when(sessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
    when(sessionContext.getPhysicalConnectionHandlingMode()).thenReturn(PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD);
    JdbcObserver jdbcObserver = Mockito.mock(JdbcObserver.class);
    when(sessionContext.getObserver()).thenReturn(jdbcObserver);
    JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
    when(serviceRegistry.getService(eq(JdbcServices.class))).thenReturn(jdbcServices);
    SqlExceptionHelper sqlExceptionHelper = Mockito.mock(SqlExceptionHelper.class);
    when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
    JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(null, sessionOwner);
    Batch currentBatch = Mockito.mock(Batch.class);
    Field currentBatchField = JdbcCoordinatorImpl.class.getDeclaredField("currentBatch");
    currentBatchField.setAccessible(true);
    currentBatchField.set(jdbcCoordinator, currentBatch);
    doThrow(IllegalStateException.class).when(currentBatch).release();
    try {
        jdbcCoordinator.close();
        fail("Should throw IllegalStateException");
    } catch (Exception expected) {
        assertEquals(IllegalStateException.class, expected.getClass());
    }
    verify(jdbcConnectionAccess, times(1)).releaseConnection(same(connection));
}
Also used : JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) SQLException(java.sql.SQLException) Field(java.lang.reflect.Field) JdbcObserver(org.hibernate.resource.jdbc.spi.JdbcObserver) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 15 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class SubclassProxyInterfaceTest method testSubclassProxyInterfaces.

@Test
public void testSubclassProxyInterfaces() {
    final Configuration cfg = new Configuration().setProperty(Environment.DIALECT, H2Dialect.class.getName()).addClass(Person.class);
    ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
    cfg.buildSessionFactory(serviceRegistry).close();
    ServiceRegistryBuilder.destroy(serviceRegistry);
}
Also used : Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Aggregations

ServiceRegistry (org.hibernate.service.ServiceRegistry)48 Test (org.junit.Test)27 MetadataSources (org.hibernate.boot.MetadataSources)18 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)17 Configuration (org.hibernate.cfg.Configuration)14 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)11 AnnotationException (org.hibernate.AnnotationException)6 MappingException (org.hibernate.MappingException)6 SessionFactory (org.hibernate.SessionFactory)6 Metadata (org.hibernate.boot.Metadata)6 TestForIssue (org.hibernate.testing.TestForIssue)5 Map (java.util.Map)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)4 HibernateException (org.hibernate.HibernateException)3 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)3 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)3 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2