Search in sources :

Example 1 with SchemaManagementTool

use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.

the class SchemaExport method doExecution.

public void doExecution(Action action, boolean needsJdbc, Metadata metadata, ServiceRegistry serviceRegistry, TargetDescriptor targetDescriptor) {
    Map config = new HashMap();
    config.putAll(serviceRegistry.getService(ConfigurationService.class).getSettings());
    config.put(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
    config.put(AvailableSettings.FORMAT_SQL, format);
    config.put(AvailableSettings.HBM2DDL_IMPORT_FILES, importFiles);
    final SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);
    final ExceptionHandler exceptionHandler = haltOnError ? ExceptionHandlerHaltImpl.INSTANCE : new ExceptionHandlerCollectingImpl();
    final ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, exceptionHandler);
    final SourceDescriptor sourceDescriptor = new SourceDescriptor() {

        @Override
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        @Override
        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    };
    try {
        if (action.doDrop()) {
            tool.getSchemaDropper(config).doDrop(metadata, executionOptions, sourceDescriptor, targetDescriptor);
        }
        if (action.doCreate()) {
            tool.getSchemaCreator(config).doCreation(metadata, executionOptions, sourceDescriptor, targetDescriptor);
        }
    } finally {
        if (exceptionHandler instanceof ExceptionHandlerCollectingImpl) {
            exceptions.addAll(((ExceptionHandlerCollectingImpl) exceptionHandler).getExceptions());
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) SourceDescriptor(org.hibernate.tool.schema.spi.SourceDescriptor) HashMap(java.util.HashMap) ExceptionHandlerCollectingImpl(org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SchemaManagementTool

use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.

the class SchemaManagementToolInitiator method initiateService.

public SchemaManagementTool initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final Object setting = configurationValues.get(AvailableSettings.SCHEMA_MANAGEMENT_TOOL);
    SchemaManagementTool tool = registry.getService(StrategySelector.class).resolveStrategy(SchemaManagementTool.class, setting);
    if (tool == null) {
        tool = new HibernateSchemaManagementTool();
    }
    return tool;
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector)

Example 3 with SchemaManagementTool

use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.

the class SchemaToolTransactionHandlingTest method testDropCreateDropInExistingJtaTransaction.

// for each case we want to run these tool delegates in a matrix of:
//		1) JTA versus JDBC transaction handling
//		2) existing transaction versus not
//
// cases:
//		1) create-drop
//		2) update
//		3) validate
//
// so:
//		1) create-drop
//			1.1) JTA transaction handling
//				1.1.1) inside an existing transaction
//				1.1.2) outside any transaction
//			1.1) JDBC transaction handling
//				- there really cannot be an "existing transaction" case...
@Test
public void testDropCreateDropInExistingJtaTransaction() {
    // 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 SchemaDropper schemaDropper = smt.getSchemaDropper(Collections.emptyMap());
        final SchemaCreator schemaCreator = smt.getSchemaCreator(Collections.emptyMap());
        final Metadata mappings = buildMappings(registry);
        try {
            try {
                schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
            } catch (CommandAcceptanceException e) {
            //ignore may happen if sql drop does not support if exist
            }
            schemaCreator.doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
        } finally {
            try {
                schemaDropper.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) SchemaCreator(org.hibernate.tool.schema.spi.SchemaCreator) Metadata(org.hibernate.boot.Metadata) SchemaDropper(org.hibernate.tool.schema.spi.SchemaDropper) CommandAcceptanceException(org.hibernate.tool.schema.spi.CommandAcceptanceException) 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)

Example 4 with SchemaManagementTool

use of org.hibernate.tool.schema.spi.SchemaManagementTool 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)

Example 5 with SchemaManagementTool

use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.

the class SchemaUpdate method execute.

@SuppressWarnings("unchecked")
public void execute(EnumSet<TargetType> targetTypes, Metadata metadata, ServiceRegistry serviceRegistry) {
    if (targetTypes.isEmpty()) {
        LOG.debug("Skipping SchemaExport as no targets were specified");
        return;
    }
    exceptions.clear();
    LOG.runningHbm2ddlSchemaUpdate();
    Map config = new HashMap();
    config.putAll(serviceRegistry.getService(ConfigurationService.class).getSettings());
    config.put(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
    config.put(AvailableSettings.FORMAT_SQL, format);
    final SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);
    final ExceptionHandlerCollectingImpl exceptionHandler = new ExceptionHandlerCollectingImpl();
    final ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, exceptionHandler);
    final TargetDescriptor targetDescriptor = SchemaExport.buildTargetDescriptor(targetTypes, outputFile, serviceRegistry);
    try {
        tool.getSchemaMigrator(config).doMigration(metadata, executionOptions, targetDescriptor);
    } finally {
        exceptions.addAll(exceptionHandler.getExceptions());
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) HashMap(java.util.HashMap) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandlerCollectingImpl(org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SchemaManagementTool (org.hibernate.tool.schema.spi.SchemaManagementTool)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExecutionOptions (org.hibernate.tool.schema.spi.ExecutionOptions)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 SystemException (javax.transaction.SystemException)2 Transaction (javax.transaction.Transaction)2 Metadata (org.hibernate.boot.Metadata)2 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)2 StandardServiceRegistryImpl (org.hibernate.boot.registry.internal.StandardServiceRegistryImpl)2 ExceptionHandlerCollectingImpl (org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl)2 CommandAcceptanceException (org.hibernate.tool.schema.spi.CommandAcceptanceException)2 Test (org.junit.Test)2 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)1 ExceptionHandler (org.hibernate.tool.schema.spi.ExceptionHandler)1 SchemaCreator (org.hibernate.tool.schema.spi.SchemaCreator)1 SchemaDropper (org.hibernate.tool.schema.spi.SchemaDropper)1 SourceDescriptor (org.hibernate.tool.schema.spi.SourceDescriptor)1 TargetDescriptor (org.hibernate.tool.schema.spi.TargetDescriptor)1