Example usage for javax.persistence Persistence generateSchema

List of usage examples for javax.persistence Persistence generateSchema

Introduction

In this page you can find the example usage for javax.persistence Persistence generateSchema.

Prototype

public static void generateSchema(String persistenceUnitName, Map map) 

Source Link

Document

Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.

Usage

From source file:br.ufc.deti.ecgweb.Application.java

public static void main(String args[]) {

    Persistence.generateSchema("ecgweb", null);
    SpringApplication.run(Application.class, args);
}

From source file:io.github.divinespear.maven.plugin.JpaSchemaGeneratorMojo.java

private void generate() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    /*//from  w  w w.  j  a v a2  s. c o  m
     * Common JPA options
     */
    // mode
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_DATABASE_ACTION, this.databaseAction.toLowerCase());
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION, this.scriptAction.toLowerCase());
    // output files
    if (this.isScriptTarget()) {
        if (this.outputDirectory == null) {
            throw new NullArgumentException("outputDirectory is required for script generation.");
        }
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
                this.getCreateOutputFile().toURI().toString());
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
                this.getDropOutputFile().toURI().toString());

    }
    // database emulation options
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME, this.databaseProductName);
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION,
            this.databaseMajorVersion == null ? null : String.valueOf(this.databaseMajorVersion));
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION,
            this.databaseMinorVersion == null ? null : String.valueOf(this.databaseMinorVersion));
    // database options
    map.put(PersistenceUnitProperties.JDBC_DRIVER, this.jdbcDriver);
    map.put(PersistenceUnitProperties.JDBC_URL, this.jdbcUrl);
    map.put(PersistenceUnitProperties.JDBC_USER, this.jdbcUser);
    map.put(PersistenceUnitProperties.JDBC_PASSWORD, this.jdbcPassword);
    // source selection
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SOURCE, this.createSourceMode);
    if (this.createSourceFile == null) {
        if (!PersistenceUnitProperties.SCHEMA_GENERATION_METADATA_SOURCE.equals(this.createSourceMode)) {
            throw new IllegalArgumentException(
                    "create source file is required for mode " + this.createSourceMode);
        }
    } else {
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SCRIPT_SOURCE,
                this.createSourceFile.toURI().toString());
    }
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SOURCE, this.dropSourceMode);
    if (this.dropSourceFile == null) {
        if (!PersistenceUnitProperties.SCHEMA_GENERATION_METADATA_SOURCE.equals(this.dropSourceMode)) {
            throw new IllegalArgumentException("drop source file is required for mode " + this.dropSourceMode);
        }
    } else {
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SCRIPT_SOURCE,
                this.dropSourceFile.toURI().toString());
    }

    /*
     * EclipseLink specific
     */
    // persistence.xml
    map.put(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, this.persistenceXml);

    /*
     * Hibernate specific
     */
    // naming strategy
    map.put(AvailableSettings.NAMING_STRATEGY, this.namingStrategy);
    // auto-detect
    map.put(AvailableSettings.AUTODETECTION, "class,hbm");
    // dialect (without jdbc connection)
    if (this.dialect == null && this.jdbcUrl == null) {
        DialectResolutionInfo info = new DialectResolutionInfo() {
            @Override
            public String getDriverName() {
                return null;
            }

            @Override
            public int getDriverMinorVersion() {
                return 0;
            }

            @Override
            public int getDriverMajorVersion() {
                return 0;
            }

            @Override
            public String getDatabaseName() {
                return databaseProductName;
            }

            @Override
            public int getDatabaseMinorVersion() {
                return databaseMinorVersion;
            }

            @Override
            public int getDatabaseMajorVersion() {
                return databaseMajorVersion;
            }
        };
        Dialect detectedDialect = StandardDialectResolver.INSTANCE.resolveDialect(info);
        this.dialect = detectedDialect.getClass().getName();
    }
    if (this.dialect != null) {
        map.put(org.hibernate.cfg.AvailableSettings.DIALECT, this.dialect);
    }

    if (!this.isDatabaseTarget() && StringUtils.isEmpty(this.jdbcUrl)) {
        map.put(AvailableSettings.SCHEMA_GEN_CONNECTION, new ConnectionMock(this.getDatabaseProductName(),
                this.getDatabaseMajorVersion(), this.getDatabaseMinorVersion()));
    }

    /* force override JTA to RESOURCE_LOCAL */
    map.put(PersistenceUnitProperties.TRANSACTION_TYPE, "RESOURCE_LOCAL");
    map.put(PersistenceUnitProperties.JTA_DATASOURCE, null);
    map.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, null);
    map.put(PersistenceUnitProperties.VALIDATION_MODE, "NONE");

    Persistence.generateSchema(this.persistenceUnitName, map);
}