Example usage for org.hibernate.tool.hbm2ddl SchemaUpdate setDelimiter

List of usage examples for org.hibernate.tool.hbm2ddl SchemaUpdate setDelimiter

Introduction

In this page you can find the example usage for org.hibernate.tool.hbm2ddl SchemaUpdate setDelimiter.

Prototype

public SchemaUpdate setDelimiter(String delimiter) 

Source Link

Document

Set the end of statement delimiter

Usage

From source file:com.premiumminds.persistence.utils.HibernateEnversDDL.java

License:Open Source License

private static void updateCommand(String[] args) {
    String unitName, filename = null, url, username, password;
    if (args.length < 5)
        System.out.println("Expected unitName jdbcUrl jdbcUsername jdbcPassword");
    else {/*  ww w.  j  a  v a 2 s. com*/
        unitName = args[1];
        url = args[2];
        username = args[3];
        password = args[4];
        if (args.length > 5)
            filename = args[5];

        Configuration configuration = HibernateDDL.getConfiguration(unitName);
        configuration.buildMappings();
        AuditConfiguration.getFor(configuration);
        Dialect dialect = Dialect.getDialect(configuration.getProperties());

        Connection conn = null;
        DatabaseMetadata meta = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
            meta = new DatabaseMetadata(conn, dialect, true);
            String[] updateSQL = configuration.generateSchemaUpdateScript(dialect, meta);

            HibernateDDL.stringToStream(updateSQL, filename);

            configuration.buildMappings();
            AuditConfiguration.getFor(configuration);
            SchemaUpdate su = new SchemaUpdate(configuration);
            su.setOutputFile(filename);
            su.setFormat(true);
            su.setDelimiter(";");
            su.execute(true, false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.vecna.maven.hibernate.HibernateSchemaUpdateMojo.java

License:Apache License

/**
 * Generates upgrade script for the schema.
 * {@inheritDoc}/*w  w w.  ja v a  2 s . c om*/
 */
@Override
protected void executeWithMappings(Configuration configuration)
        throws MojoExecutionException, MojoFailureException {
    SchemaUpdate schemaUpdate = new SchemaUpdate(configuration);
    schemaUpdate.setFormat(format);

    if (outputFile != null) {
        initializePath();
        schemaUpdate.setOutputFile(outputFile);
        schemaUpdate.setDelimiter(delimiter);
    }

    schemaUpdate.execute(print, export);
}

From source file:name.livitski.tools.persista.StorageBootstrap.java

License:Open Source License

@SuppressWarnings("unchecked")
private void updateSchema() throws ApplicationBeanException {
    try {// w ww.  ja  v a 2  s .co  m
        String user = readSetting(UpdaterUserNameSetting.class);
        String password;
        if (null != user)
            password = readSetting(UpdaterPasswordSetting.class);
        else {
            user = readSetting(UserNameSetting.class);
            password = readSetting(PasswordSetting.class);
        }

        org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
        cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");
        cfg.setProperty(AvailableSettings.DIALECT, readSetting(HibernateSQLDialectSetting.class).getName());
        cfg.setProperty(AvailableSettings.DRIVER, getJDBCDriverClass().getName());
        cfg.setProperty(AvailableSettings.URL, db.getMetaData().getURL());
        cfg.setProperty(AvailableSettings.USER, user);
        cfg.setProperty(AvailableSettings.PASS, password);

        for (Class<?> clazz : getEntityClasses())
            cfg.addAnnotatedClass(clazz);

        SchemaUpdate worker = new SchemaUpdate(cfg);
        worker.setDelimiter(";");
        worker.setHaltOnError(true);
        if (null != ddlDumpFile)
            worker.setOutputFile(ddlDumpFile.getAbsolutePath());
        worker.execute(true, true);
        List<Throwable> errs = (List<Throwable>) worker.getExceptions();
        if (null != errs && !errs.isEmpty())
            for (Iterator<Throwable> erri = errs.iterator();;) {
                Throwable err = erri.next();
                if (erri.hasNext())
                    log().error("", err);
                else
                    throw new SchemaUpdateException(this,
                            "Error(s) occured during the schema update, the last error is shown.", err);
            }
    } catch (ConfigurationException badConfig) {
        throw new StorageConfigurationException(this, badConfig);
    } catch (SQLException e) {
        throw new DatabaseException(this, e);
    }
}

From source file:org.dspace.app.cris.util.UpdateSchemaTool.java

private SchemaUpdate getSchemaUpdate(Configuration cfg) throws HibernateException, IOException {
    Properties properties = new Properties();
    properties.putAll(cfg.getProperties());
    if (propertiesFile == null) {
        properties.putAll(getProject().getProperties());
    } else {//  w  w w  .  ja  va  2 s .c  o  m
        properties.load(new FileInputStream(propertiesFile));
    }
    cfg.setProperties(properties);
    SchemaUpdate su = new SchemaUpdate(cfg);
    su.setOutputFile(outputFile.getPath());
    su.setDelimiter(delimiter);
    su.setHaltOnError(haltOnError);
    return su;
}

From source file:play.modules.db.Updater.java

License:Apache License

public static void main(String[] args) throws Exception {

    File root = new File(System.getProperty("application.path"));
    Play.init(root, System.getProperty("play.id", ""));
    List<Class> entities = Play.classloader.getAnnotatedClasses(Entity.class);
    AnnotationConfiguration cfg = new AnnotationConfiguration();
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");
    for (Class _class : entities) {
        cfg.addAnnotatedClass(_class);
    }/* w w w  .jav  a 2s  . c  o  m*/

    Thread.currentThread().setContextClassLoader(Play.classloader);
    final String dialect = Play.configuration.getProperty("jpa.dialect");
    if (dialect != null)
        cfg.setProperty("hibernate.dialect", dialect);

    final String driver = Play.configuration.getProperty("db.driver");
    if (driver != null)
        cfg.setProperty("hibernate.connection.driver_class", driver);

    final String user = Play.configuration.getProperty("db.user");
    if (user != null)
        cfg.setProperty("hibernate.connection.username", user);

    final String password = Play.configuration.getProperty("db.pass");
    if (password != null)
        cfg.setProperty("hibernate.connection.password", password);

    final String url = Play.configuration.getProperty("db.url");
    if (url != null)
        cfg.setProperty("hibernate.connection.url", url);

    boolean script = true;
    boolean drop = false;
    boolean create = false;
    boolean halt = false;
    boolean export = false;
    String outFile = null;
    String importFile = "/import.sql";
    String propFile = null;
    boolean format = true;
    String delim = ";";

    for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("--")) {
            if (args[i].equals("--quiet")) {
                script = false;
            } else if (args[i].equals("--drop")) {
                drop = true;
            } else if (args[i].equals("--create")) {
                create = true;
            } else if (args[i].equals("--haltonerror")) {
                halt = true;
            } else if (args[i].equals("--export")) {
                export = true;
            } else if (args[i].startsWith("--output=")) {
                outFile = args[i].substring(9);
            } else if (args[i].startsWith("--import=")) {
                importFile = args[i].substring(9);
            } else if (args[i].startsWith("--properties=")) {
                propFile = args[i].substring(13);
            } else if (args[i].equals("--noformat")) {
                format = false;
            } else if (args[i].startsWith("--delimiter=")) {
                delim = args[i].substring(12);
            } else if (args[i].startsWith("--config=")) {
                cfg.configure(args[i].substring(9));
            } else if (args[i].startsWith("--naming=")) {
                cfg.setNamingStrategy(
                        (NamingStrategy) ReflectHelper.classForName(args[i].substring(9)).newInstance());
            }
        }

    }

    if (propFile != null) {
        Properties props = new Properties();
        props.putAll(cfg.getProperties());
        props.load(new FileInputStream(propFile));
        cfg.setProperties(props);
    }

    SchemaUpdate se = new SchemaUpdate(cfg);
    se.setHaltOnError(halt);
    se.setOutputFile(outFile);
    se.setDelimiter(delim);
    if (format) {
        se.setFormat(true);
    }
    se.execute(script, false);
}