Example usage for org.hibernate.dialect Dialect toString

List of usage examples for org.hibernate.dialect Dialect toString

Introduction

In this page you can find the example usage for org.hibernate.dialect Dialect toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:be.fedict.eid.applet.maven.sql.ddl.SQLDDLMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("SQL DDL script generator");

    File outputFile = new File(this.outputDirectory, this.outputName);
    getLog().info("Output SQL DDL script file: " + outputFile.getAbsolutePath());

    this.outputDirectory.mkdirs();
    try {//from ww  w  . j a  v a 2  s .c  o  m
        outputFile.createNewFile();
    } catch (IOException e) {
        throw new MojoExecutionException("I/O error.", e);
    }

    for (ArtifactItem artifactItem : this.artifactItems) {
        getLog().info("artifact: " + artifactItem.getGroupId() + ":" + artifactItem.getArtifactId());
        List<Dependency> dependencies = this.project.getDependencies();
        String version = null;
        for (Dependency dependency : dependencies) {
            if (StringUtils.equals(dependency.getArtifactId(), artifactItem.getArtifactId())
                    && StringUtils.equals(dependency.getGroupId(), artifactItem.getGroupId())) {
                version = dependency.getVersion();
                break;
            }
        }
        getLog().info("artifact version: " + version);
        VersionRange versionRange = VersionRange.createFromVersion(version);
        Artifact artifact = this.artifactFactory.createDependencyArtifact(artifactItem.getGroupId(),
                artifactItem.getArtifactId(), versionRange, "jar", null, Artifact.SCOPE_COMPILE);
        try {
            this.resolver.resolve(artifact, this.remoteRepos, this.local);
        } catch (ArtifactResolutionException e) {
            throw new MojoExecutionException("Unable to resolve artifact.", e);
        } catch (ArtifactNotFoundException e) {
            throw new MojoExecutionException("Unable to find artifact.", e);
        }
        getLog().info("artifact file: " + artifact.getFile().getAbsolutePath());
        getLog().info("hibernate dialect: " + this.hibernateDialect);

        URL artifactUrl;
        try {
            artifactUrl = artifact.getFile().toURI().toURL();
        } catch (MalformedURLException e) {
            throw new MojoExecutionException("URL error.", e);
        }

        URLClassLoader classLoader = new URLClassLoader(new URL[] { artifactUrl },
                this.getClass().getClassLoader());
        Thread.currentThread().setContextClassLoader(classLoader);

        AnnotationDB annotationDb = new AnnotationDB();
        try {
            annotationDb.scanArchives(artifactUrl);
        } catch (IOException e) {
            throw new MojoExecutionException("I/O error.", e);
        }
        Set<String> classNames = annotationDb.getAnnotationIndex().get(Entity.class.getName());
        getLog().info("# JPA entity classes: " + classNames.size());

        AnnotationConfiguration configuration = new AnnotationConfiguration();

        configuration.setProperty("hibernate.dialect", this.hibernateDialect);
        Dialect dialect = Dialect.getDialect(configuration.getProperties());
        getLog().info("dialect: " + dialect.toString());

        for (String className : classNames) {
            getLog().info("JPA entity: " + className);
            Class<?> entityClass;
            try {
                entityClass = classLoader.loadClass(className);
                getLog().info("entity class loader: " + entityClass.getClassLoader());
            } catch (ClassNotFoundException e) {
                throw new MojoExecutionException("class not found.", e);
            }
            configuration.addAnnotatedClass(entityClass);
        }

        SchemaExport schemaExport = new SchemaExport(configuration);
        schemaExport.setFormat(true);
        schemaExport.setHaltOnError(true);
        schemaExport.setOutputFile(outputFile.getAbsolutePath());
        schemaExport.setDelimiter(";");

        try {
            getLog().info("SQL DDL script: " + IOUtil.toString(new FileInputStream(outputFile)));
        } catch (FileNotFoundException e) {
            throw new MojoExecutionException("file not found.", e);
        } catch (IOException e) {
            throw new MojoExecutionException("I/O error.", e);
        }

        // operate
        schemaExport.execute(true, false, false, true);
        List<Exception> exceptions = schemaExport.getExceptions();
        for (Exception exception : exceptions) {
            getLog().error("exception: " + exception.getMessage());
        }
    }
}

From source file:org.n52.sos.SQLScriptGenerator.java

License:Open Source License

public static void main(String[] args) {
    try {//from w  w w  .  j a  v  a  2s .  c om
        SQLScriptGenerator sqlScriptGenerator = new SQLScriptGenerator();
        Configuration configuration = new Configuration().configure("/sos-hibernate.cfg.xml");
        int dialectSelection = sqlScriptGenerator.getDialectSelection();
        Dialect dia = sqlScriptGenerator.getDialect(dialectSelection);
        int modelSelection = sqlScriptGenerator.getModelSelection();
        boolean oldConcept = sqlScriptGenerator.isOldConcept(sqlScriptGenerator.getConceptSelection());
        String schema = sqlScriptGenerator.getSchema();
        if (schema != null && !schema.isEmpty()) {
            Properties p = new Properties();
            p.put("hibernate.default_schema", schema);
            configuration.addProperties(p);
        }
        sqlScriptGenerator.setDirectoriesForModelSelection(modelSelection, oldConcept, configuration);
        // create script
        String[] create = configuration.generateSchemaCreationScript(dia);
        List<String> checkedSchema = sqlScriptGenerator.checkSchema(dia, create);
        printToScreen("Scripts are created for: " + dia.toString());
        printToScreen("");
        printToScreen("#######################################");
        printToScreen("##           Create-Script           ##");
        printToScreen("#######################################");
        printToScreen("");
        for (String t : checkedSchema) {
            printToScreen(t + ";");
        }
        // drop script
        String[] drop = configuration.generateDropSchemaScript(dia);
        List<String> checkedDrop = sqlScriptGenerator.checkSchema(dia, drop);
        printToScreen("");
        printToScreen("#######################################");
        printToScreen("##            Drop-Script            ##");
        printToScreen("#######################################");
        printToScreen("");
        for (String t : checkedDrop) {
            printToScreen(t + ";");
        }
        printToScreen("");
        printToScreen("#######################################");
    } catch (IOException ioe) {
        printToScreen("ERROR: IO error trying to read your input!");
        System.exit(1);
    } catch (MissingDriverException mde) {
        System.exit(1);
    } catch (Exception e) {
        printToScreen("ERROR: " + e.getMessage());
        System.exit(1);
    }

}

From source file:org.squale.squalecommon.util.database.DatabaseTypeFactory.java

License:Open Source License

/**
 * Create the good new instance of DatabaseType according to the Database used.
 * //from   w  w  w .j  a va2s . co m
 * @throws JrafDaoException This exception happened if the dialect used for configure hibernate is not implement in
 *             Squale
 */
private static void newInstance() throws JrafDaoException {
    PersistenceProviderImpl session = (PersistenceProviderImpl) PersistenceHelper.getPersistenceProvider();
    SessionFactoryImpl hibSession = (SessionFactoryImpl) session.getSessions();
    Dialect dial = hibSession.getDialect();
    String dialectUsed = dial.toString();

    if (dialectUsed.startsWith("org.hibernate.dialect.MySQL")) {
        databaseInstance = new MySQLType();
    } else if (dialectUsed.startsWith("org.hibernate.dialect.Oracle")) {
        databaseInstance = new OracleType();
    } else if (dialectUsed.startsWith("org.hibernate.dialect.HSQL")) {
        databaseInstance = new HsqldbType();
    } else {
        throw new JrafDaoException("hibernate dialect unknown, contact the Squale administrator");
    }
}