Example usage for org.hibernate.jpa AvailableSettings SCHEMA_GEN_CONNECTION

List of usage examples for org.hibernate.jpa AvailableSettings SCHEMA_GEN_CONNECTION

Introduction

In this page you can find the example usage for org.hibernate.jpa AvailableSettings SCHEMA_GEN_CONNECTION.

Prototype

String SCHEMA_GEN_CONNECTION

To view the source code for org.hibernate.jpa AvailableSettings SCHEMA_GEN_CONNECTION.

Click Source Link

Usage

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

License:Apache License

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

    /*//from  w ww .j  a  v a 2s.c  om
     * 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);
}

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

License:Apache License

@SuppressWarnings("deprecation")
public static Map<String, Object> buildProperties(JpaSchemaGeneratorMojo mojo) {
    Map<String, Object> map = new HashMap<>();
    Map<String, String> properties = mojo.getProperties();

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

    }
    // database emulation options
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME, mojo.getDatabaseProductName());
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION,
            mojo.getDatabaseMajorVersion() == null ? null : String.valueOf(mojo.getDatabaseMajorVersion()));
    map.put(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION,
            mojo.getDatabaseMinorVersion() == null ? null : String.valueOf(mojo.getDatabaseMinorVersion()));
    // database options
    map.put(PersistenceUnitProperties.JDBC_DRIVER, mojo.getJdbcDriver());
    map.put(PersistenceUnitProperties.JDBC_URL, mojo.getJdbcUrl());
    map.put(PersistenceUnitProperties.JDBC_USER, mojo.getJdbcUser());
    map.put(PersistenceUnitProperties.JDBC_PASSWORD, mojo.getJdbcPassword());
    // source selection
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SOURCE, mojo.getCreateSourceMode());
    if (mojo.getCreateSourceFile() == null) {
        if (!PersistenceUnitProperties.SCHEMA_GENERATION_METADATA_SOURCE.equals(mojo.getCreateSourceMode())) {
            throw new IllegalArgumentException(
                    "create source file is required for mode " + mojo.getCreateSourceMode());
        }
    } else {
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SCRIPT_SOURCE,
                mojo.getCreateSourceFile().toURI().toString());
    }
    map.put(PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SOURCE, mojo.getDropSourceMode());
    if (mojo.getDropSourceFile() == null) {
        if (!PersistenceUnitProperties.SCHEMA_GENERATION_METADATA_SOURCE.equals(mojo.getDropSourceMode())) {
            throw new IllegalArgumentException(
                    "drop source file is required for mode " + mojo.getDropSourceMode());
        }
    } else {
        map.put(PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SCRIPT_SOURCE,
                mojo.getDropSourceFile().toURI().toString());
    }

    /*
     * EclipseLink specific
     */
    // persistence.xml
    map.put(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, mojo.getPersistenceXml());
    // disable weaving
    map.put(PersistenceUnitProperties.WEAVING, "false");

    /*
     * Hibernate specific
     */
    // auto-detect
    map.put(AvailableSettings.AUTODETECTION, "class,hbm");
    // dialect (without jdbc connection)
    String dialect = properties.get(org.hibernate.cfg.AvailableSettings.DIALECT);
    if (StringUtils.isEmpty(dialect) && StringUtils.isEmpty(mojo.getJdbcUrl())) {
        final String productName = mojo.getDatabaseProductName();
        final int minorVersion = mojo.getDatabaseMinorVersion() == null ? 0 : mojo.getDatabaseMinorVersion();
        final int majorVersion = mojo.getDatabaseMajorVersion() == null ? 0 : mojo.getDatabaseMajorVersion();
        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 productName;
            }

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

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

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

    map.putAll(mojo.getProperties());

    /* 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");

    // normalize - remove null
    List<String> keys = new ArrayList<>(map.keySet());
    for (String key : keys) {
        if (map.get(key) == null) {
            map.remove(key);
        }
    }

    return map;
}