Search in sources :

Example 66 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project jOOQ by jOOQ.

the class JPADatabase method create0.

@SuppressWarnings("serial")
@Override
protected DSLContext create0() {
    if (connection == null) {
        String packages = getProperties().getProperty("packages");
        if (isBlank(packages)) {
            packages = "";
            log.warn("No packages defined", "It is highly recommended that you provide explicit packages to scan");
        }
        try {
            connection = DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", "");
            MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect").applySetting("javax.persistence.schema-generation-connection", connection).applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {

                @SuppressWarnings("rawtypes")
                @Override
                public boolean isUnwrappableAs(Class unwrapType) {
                    return false;
                }

                @Override
                public <T> T unwrap(Class<T> unwrapType) {
                    return null;
                }

                @Override
                public Connection getConnection() {
                    return connection;
                }

                @Override
                public void closeConnection(Connection conn) throws SQLException {
                }

                @Override
                public boolean supportsAggressiveRelease() {
                    return true;
                }
            }).build());
            ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
            scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
            // [#5845] Use the correct ClassLoader to load the jpa entity classes defined in the user project
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            for (String pkg : packages.split(",")) for (BeanDefinition def : scanner.findCandidateComponents(defaultIfBlank(pkg, "").trim())) metadata.addAnnotatedClass(Class.forName(def.getBeanClassName(), true, cl));
            // This seems to be the way to do this in idiomatic Hibernate 5.0 API
            // See also: http://stackoverflow.com/q/32178041/521799
            // SchemaExport export = new SchemaExport((MetadataImplementor) metadata.buildMetadata(), connection);
            // export.create(true, true);
            // Hibernate 5.2 broke 5.0 API again. Here's how to do this now:
            SchemaExport export = new SchemaExport();
            export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
        } catch (Exception e) {
            throw new DataAccessException("Error while exporting schema", e);
        }
    }
    return DSL.using(connection);
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) MetadataSources(org.hibernate.boot.MetadataSources) Connection(java.sql.Connection) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) DataAccessException(org.jooq.exception.DataAccessException) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 67 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project ACS by ACS-Community.

the class TestCase method runSchemaGeneration.

protected void runSchemaGeneration() {
    SchemaExport export = new SchemaExport(cfg);
    export.create(true, true);
}
Also used : SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 68 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project SimpleContentPortlet by Jasig.

the class SchemaCreator method create.

private int create() {
    /*
         * We will need to provide a Configuration and a Connection;  both should be properly
         * managed by the Spring ApplicationContext.
         */
    final DataSource dataSource = applicationContext.getBean(DATA_SOURCE_BEAN_NAME, DataSource.class);
    try (final Connection conn = dataSource.getConnection()) {
        final Configuration config = new Configuration();
        config.setProperty("hibernate.dialect", hibernateDialect);
        config.addAnnotatedClass(Attachment.class);
        logger.info("Creating database schema based on the following Configuration:\n{}", config);
        final SchemaExport schemaExport = new SchemaExport(config, conn);
        schemaExport.execute(true, true, false, false);
        final List<Exception> exceptions = schemaExport.getExceptions();
        if (exceptions.size() != 0) {
            logger.error("Schema Create Failed;  see below for details");
            for (Exception e : exceptions) {
                logger.error("Exception from Hibernate Tools SchemaExport", e);
            }
            return 1;
        }
    } catch (SQLException sqle) {
        logger.error("Failed to initialize & invoke the SchemaExport tool", sqle);
        return 1;
    }
    return 0;
}
Also used : Configuration(org.hibernate.cfg.Configuration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BeansException(org.springframework.beans.BeansException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 69 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newSchemaExport.

@Override
public ISchemaExport newSchemaExport(IConfiguration hcfg) {
    ISchemaExport result = null;
    if (hcfg instanceof IFacade) {
        SchemaExport schemaExport = new SchemaExport((Configuration) ((IFacade) hcfg).getTarget());
        result = facadeFactory.createSchemaExport(schemaExport);
    }
    return result;
}
Also used : ISchemaExport(org.jboss.tools.hibernate.runtime.spi.ISchemaExport) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) ISchemaExport(org.jboss.tools.hibernate.runtime.spi.ISchemaExport) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 70 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method newSchemaExport.

@Override
public ISchemaExport newSchemaExport(IConfiguration hcfg) {
    ISchemaExport result = null;
    if (hcfg instanceof IFacade) {
        Configuration configuration = (Configuration) ((IFacade) hcfg).getTarget();
        MetadataImplementor metadata = (MetadataImplementor) MetadataHelper.getMetadata(configuration);
        SchemaExport schemaExport = new SchemaExport(metadata);
        result = facadeFactory.createSchemaExport(schemaExport);
    }
    return result;
}
Also used : Configuration(org.hibernate.cfg.Configuration) JDBCMetaDataConfiguration(org.hibernate.cfg.JDBCMetaDataConfiguration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) ISchemaExport(org.jboss.tools.hibernate.runtime.spi.ISchemaExport) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) ISchemaExport(org.jboss.tools.hibernate.runtime.spi.ISchemaExport) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Aggregations

SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)90 Test (org.junit.Test)45 MetadataSources (org.hibernate.boot.MetadataSources)42 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)28 ISchemaExport (org.jboss.tools.hibernate.runtime.spi.ISchemaExport)27 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)22 TestForIssue (org.hibernate.testing.TestForIssue)13 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)12 Configuration (org.hibernate.cfg.Configuration)12 Connection (java.sql.Connection)10 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)10 After (org.junit.After)10 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)9 File (java.io.File)8 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 Metadata (org.hibernate.boot.Metadata)5 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)5 SQLException (java.sql.SQLException)4 Entity (javax.persistence.Entity)4