Example usage for org.hibernate.cfg AvailableSettings HBM2DDL_IMPORT_FILES

List of usage examples for org.hibernate.cfg AvailableSettings HBM2DDL_IMPORT_FILES

Introduction

In this page you can find the example usage for org.hibernate.cfg AvailableSettings HBM2DDL_IMPORT_FILES.

Prototype

String HBM2DDL_IMPORT_FILES

To view the source code for org.hibernate.cfg AvailableSettings HBM2DDL_IMPORT_FILES.

Click Source Link

Document

Comma-separated names of the optional files containing SQL DML statements executed during the SessionFactory creation.

Usage

From source file:com.github.antennaesdk.messageserver.db.H2.H2Database.java

License:Apache License

public void generateSchemaAndCreateTables(SimpleDriverDataSource dataSource) {

    // Get the tables that are already in the DATABASE
    List<String> tables = new ArrayList<>();
    try {//w w w  .  j a v  a 2s  .  c  om
        Connection connection = dataSource.getConnection();
        DatabaseMetaData databaseMetadata = connection.getMetaData();
        ResultSet resultSet = databaseMetadata.getTables(null, null, null, new String[] { "TABLE" });
        while (resultSet.next()) {
            String table = resultSet.getString(3);
            logger.info("Table : " + table + " ... exists");
            tables.add(table);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    // Get the tables that are needed from Entity Classes
    List<Class> tablesToCreate = new ArrayList<>();
    for (Class<?> c : entityClasses) {
        // get the table names
        Table table = c.getAnnotation(Table.class);

        logger.info("Entity: " + c.getName() + " , Table: " + table.name());
        boolean isExisting = false;
        for (String dbTable : tables) {
            if (dbTable.equals(table.name())) {
                isExisting = true;
                break;
            }
        }

        if (!isExisting) {
            // these tables must be created
            tablesToCreate.add(c);
        }
    }

    // Check whether the tables need to be created...
    if (tablesToCreate.size() == 0) {
        logger.info("Tables already exist... ");
        return;
    } else {
        logger.info("Creating tables...");
    }

    //create a minimal configuration
    org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");

    // create a temporary file to write the DDL
    File ddlFile = null;
    try {
        File dir = getDirectoryFromClasspath();
        ddlFile = File.createTempFile("H2_", ".SQL", dir);
        ddlFile.deleteOnExit();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // add the tables to be created
    for (Class c : tablesToCreate) {
        cfg.addAnnotatedClass(c);
    }

    //build all the mappings, before calling the AuditConfiguration
    cfg.buildMappings();
    cfg.getProperties().setProperty(AvailableSettings.HBM2DDL_IMPORT_FILES, ddlFile.getName());

    cfg.getProperties().setProperty("hibernate.connection.driver_class", "org.h2.Driver");
    cfg.getProperties().setProperty("hibernate.connection.url", dataSource.getUrl());
    cfg.getProperties().setProperty("hibernate.connection.username", dataSource.getUsername());
    cfg.getProperties().setProperty("hibernate.connection.password", dataSource.getPassword());

    //execute the export
    SchemaExport export = new SchemaExport(cfg);

    export.setDelimiter(";");
    export.setFormat(true);
    // create the tables in the DB and show the DDL in console
    export.create(true, true);
}

From source file:com.okasamastarr.CreateDdlMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {//from w  w w. jav a  2  s . c  o m
        Thread.currentThread().setContextClassLoader(createProjectClassLoader());
        Configuration cfg = createHibernateConfiguration(persistenceUnit);

        Properties props = new Properties();
        if (propFile != null) {
            props.load(new FileInputStream(propFile));
        }
        cfg.setProperties(props);
        if (dialect != null) {
            props.setProperty("hibernate.dialect", dialect);
        }
        if (importFile != null) {
            props.setProperty(AvailableSettings.HBM2DDL_IMPORT_FILES, importFile);
        }

        SchemaExport schemaExport = new SchemaExport(cfg).setOutputFile(outputFile).setFormat(format)
                .setDelimiter(delimeter);
        schemaExport.execute(script, export, drop, create);
    } catch (Throwable ex) {
        throw new MojoExecutionException("Failed to create DDL schema", ex);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}