Example usage for org.hibernate.tool.hbm2ddl SchemaExport setOutputFile

List of usage examples for org.hibernate.tool.hbm2ddl SchemaExport setOutputFile

Introduction

In this page you can find the example usage for org.hibernate.tool.hbm2ddl SchemaExport setOutputFile.

Prototype

public SchemaExport setOutputFile(String filename) 

Source Link

Document

For generating a export script file, this is the file which will be written.

Usage

From source file:org.spliffy.server.db.utils.SchemaExporter.java

License:Open Source License

/**
 * Method that actually creates the file.
 *
 * @param dbDialect to use//  w w  w  .j a va 2s . com
 */
public void generate() throws Exception {
    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }
    AnnotationConfiguration cfg;
    cfg = new AnnotationConfiguration();
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");
    cfg.setNamingStrategy(new org.hibernate.cfg.ImprovedNamingStrategy());

    for (Class<Object> clazz : getClasses(packageName)) {
        cfg.addAnnotatedClass(clazz);
    }
    for (Dialect d : Dialect.values()) {
        cfg.setProperty("hibernate.dialect", d.getDialectClass());
        SchemaExport export = new SchemaExport(cfg);
        export.setDelimiter(";");
        File fOut = new File(outputDir, d.name().toLowerCase() + ".sql");
        export.setOutputFile(fOut.getAbsolutePath());
        export.execute(true, false, false, false);
    }
}

From source file:org.springframework.cloud.dataflow.server.repository.SchemaGenerationTests.java

License:Apache License

private void generateDdlFiles(String dialect, File tempDir, PersistenceUnitInfo persistenceUnitInfo) {
    logger.info("Generating DDL script for " + dialect);

    final MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder()
            .applySetting("hibernate.dialect", "org.hibernate.dialect." + dialect + "Dialect")
            .applySetting("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName())
            .applySetting("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName())
            .build());//from   w  w w. j  av a2 s  .c  om

    for (String clazz : persistenceUnitInfo.getManagedClassNames()) {
        logger.info(clazz);
        metadata.addAnnotatedClassName(clazz);
    }

    final SchemaExport export;
    try {
        export = new SchemaExport();
        export.setDelimiter(";");
        export.setFormat(true);
        export.setOutputFile(new File(tempDir, "schema-" + dialect.toLowerCase() + ".sql").getAbsolutePath());
    } catch (HibernateException e) {
        throw new IllegalStateException(e);
    }
    EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
    export.execute(targetTypes, SchemaExport.Action.BOTH, metadata.buildMetadata());
}

From source file:org.stanwood.media.database.DBHelper.java

License:Open Source License

/**
 * Uses to get the database schema for a given dialect
 * @param dialect The dialect//from w w w . j  av  a2 s .  c  o m
 * @return The schema
 * @throws DatabaseException Thrown if their is a problem with hibernate
 */
public String getSchema(String dialect) throws DatabaseException {
    try {
        Configuration config = getConfiguration("", "", "", dialect, SchemaCheck.NONE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        SchemaExport exporter = new SchemaExport(config);
        exporter.setFormat(true);
        File file = FileHelper.createTempFile("schema", ".sql"); //$NON-NLS-1$//$NON-NLS-2$
        try {
            exporter.setOutputFile(file.getAbsolutePath());
            exporter.create(true, false);
            return FileHelper.readFileContents(file);
        } finally {
            FileHelper.delete(file);
        }
    } catch (HibernateException e) {
        throw new DatabaseException(Messages.getString("DBHelper.UnablePrintSchema"), e); //$NON-NLS-1$
    } catch (XMLParserException e) {
        throw new DatabaseException(Messages.getString("DBHelper.UnablePrintSchema"), e); //$NON-NLS-1$
    } catch (IOException e) {
        throw new DatabaseException(Messages.getString("DBHelper.UnablePrintSchema"), e); //$NON-NLS-1$
    }
}

From source file:org.transitime.applications.SchemaGenerator.java

License:Open Source License

/**
 * Method that actually creates the file.
 * //  www .j  a v a  2  s .  co m
 * @param dbDialect to use
 */
private void generate(Dialect dialect) {
    cfg.setProperty("hibernate.dialect", dialect.getDialectClass());

    SchemaExport export = new SchemaExport(cfg);
    export.setDelimiter(";");

    // Determine file name. Use "ddl_" plus dialect name such as mysql or
    // oracle plus the package name with "_" replacing "." such as
    // org_transitime_db_structs .
    String packeNameSuffix = packageName.replace(".", "_");
    String outputFilename = (outputDirectory != null ? outputDirectory + "/" : "") + "ddl_"
            + dialect.name().toLowerCase() + "_" + packeNameSuffix + ".sql";

    export.setOutputFile(outputFilename);

    // Export, but only to an SQL file. Don't actually modify the database
    System.out.println("Writing file " + outputFilename);
    export.execute(true, false, false, false);

    // Get rid of unneeded SQL for dropping tables and keys and such
    trimCruftFromFile(outputFilename);
}

From source file:persistence.DBUtility.java

public static void schemaExport() {
    Configuration config = new Configuration().configure("hibernate.cfg.xml");
    SchemaExport exporter = new SchemaExport(config);
    exporter.setDelimiter(";");
    exporter.setOutputFile("pentamatrix-2.sql");
    exporter.execute(Target.EXPORT, SchemaExport.Type.BOTH);
}

From source file:ru.appliedtech.storage.hibernate.DDLGeneratorUtil.java

License:Open Source License

public static void execute(String dialectClassName, String packageName, String outputFilePath) {
    Configuration configuration = new Configuration();
    configuration.addPackage(packageName);
    configuration.setProperty(Environment.DIALECT, dialectClassName);
    Collection<Class<? extends Object>> classes = getPackageClasses(packageName);
    for (Class<?> entityClass : classes) {
        configuration.addAnnotatedClass(entityClass);
    }//w w  w . j  a va2  s .  c  o m
    SchemaExport schemaExport = new SchemaExport(configuration);
    schemaExport.setDelimiter(";");
    schemaExport.setOutputFile(outputFilePath);
    schemaExport.create(true, false);
}

From source file:sk.lazyman.gizmo.SpringApplicationContextTest.java

License:Apache License

private void createSQLSchema(String fileName) throws Exception {
    org.hibernate.cfg.Configuration configuration = new Configuration();
    Properties properties = new Properties();
    properties.putAll(sessionFactoryBean.getJpaPropertyMap());
    configuration.setProperties(properties);
    configuration.setNamingStrategy(new GizmoNamingStrategy());

    System.out.println("Dialect: " + properties.getProperty("hibernate.dialect"));

    addAnnotatedClasses("sk.lazyman.gizmo.data", configuration);

    SchemaExport export = new SchemaExport(configuration);
    export.setOutputFile(fileName);
    export.setDelimiter(";");
    export.execute(true, false, false, true);
}

From source file:uk.co.modularaudio.util.hibernate.generator.GeneratorHelper.java

License:Open Source License

/**
 * Method to configure necessary hibernate properties and generate DDL for a supplied configuration
 * @param dialectName Which hibernate dialect should be used
 * @param destinationDirectory The output directory
 * @param outputFileName The output filename
 * @param configuration The hibernate configuration setup with the appropriate schema objects
 *///from  w  w w  . j a  v a  2  s .  c  o m
private void configureAndGenerate(final String dialectName, final String destinationDirectory,
        final String outputFileName, final Configuration configuration) {
    final Properties dialect = new Properties();
    dialect.setProperty("hibernate.dialect", dialectName);
    configuration.addProperties(dialect);

    final SchemaExport se = new SchemaExport(configuration);
    se.setOutputFile(destinationDirectory + outputFileName);
    se.setDelimiter(";\n");
    se.create(true, false);
}

From source file:util.HibernateDDLGenerator.java

private void execute(Dialect dialect, Class<?>... classes) {
    AnnotationConfiguration configuration = new AnnotationConfiguration();
    configuration.setProperty(Environment.DIALECT, dialect.getClassName());
    for (Class<?> entityClass : classes) {
        configuration.addAnnotatedClass(entityClass);
    }//from   w  w w.j a v  a  2s .  com
    configuration.configure("hibernate.cfg.xml");
    SchemaExport schemaExport = new SchemaExport(configuration);
    schemaExport.setDelimiter(";");
    schemaExport.setOutputFile(
            String.format("%s_%s.%s ", new Object[] { "ddl", dialect.name().toLowerCase(), "sql" }));
    boolean consolePrint = true;
    boolean exportInDatabase = true;
    schemaExport.create(consolePrint, exportInDatabase);
}