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

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

Introduction

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

Prototype

public SchemaUpdate setHaltOnError(boolean haltOnError) 

Source Link

Usage

From source file:lucee.runtime.orm.hibernate.HibernateSessionFactory.java

License:Open Source License

private static void schemaExport(Log log, Configuration configuration, DatasourceConnection dc,
        SessionFactoryData data) throws PageException, SQLException, IOException {
    ORMConfiguration ormConf = data.getORMConfiguration();

    if (ORMConfiguration.DBCREATE_NONE == ormConf.getDbCreate()) {
        return;//from  w w w  .j  a  va  2 s  .c om
    } else if (ORMConfiguration.DBCREATE_DROP_CREATE == ormConf.getDbCreate()) {
        SchemaExport export = new SchemaExport(configuration);
        export.setHaltOnError(true);

        export.execute(false, true, false, false);
        printError(log, data, export.getExceptions(), false);
        executeSQLScript(ormConf, dc);
    } else if (ORMConfiguration.DBCREATE_UPDATE == ormConf.getDbCreate()) {
        SchemaUpdate update = new SchemaUpdate(configuration);
        update.setHaltOnError(true);
        update.execute(false, true);
        printError(log, data, update.getExceptions(), false);
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void updateSchema() throws ApplicationBeanException {
    try {//from w w w  .  java2  s .  c  o 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 ww  . j  av  a  2s . 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);
    }//from  w w w.  j  av a  2s .  com

    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);
}