List of usage examples for org.hibernate.tool.hbm2ddl SchemaExport setDelimiter
public SchemaExport setDelimiter(String delimiter)
From source file:com.oneandone.relesia.tools.SQLScriptGenerator.java
License:Apache License
public static void main(String[] args) throws MappingException, IOException { String createSQLFile = "dbscripts/createTables.sql"; String dropSQLFile = "dbscripts/dropTables.sql"; String hibernateCfgFile = "/db/hibernate.cfg.xml"; final EnumSet<TargetType> targetTypes = EnumSet.noneOf(TargetType.class); targetTypes.add(TargetType.SCRIPT);//from w ww . ja v a 2 s . co m System.out.println("Initialize Hibernate configuration from " + hibernateCfgFile); Configuration cfg = new Configuration().configure(hibernateCfgFile); Metadata metadata = MetadataHelper.getMetadata(cfg); SchemaExport export = new SchemaExport(); export.setHaltOnError(true); export.setFormat(true); export.setDelimiter(";"); System.out.println("Generating create SQL to file " + createSQLFile); if (new File(createSQLFile).exists()) { Files.delete(Paths.get(createSQLFile)); } export.setOutputFile(createSQLFile); export.execute(targetTypes, Action.CREATE, metadata); System.out.println("Generating drop SQL to file " + dropSQLFile); export.setOutputFile(dropSQLFile); if (new File(dropSQLFile).exists()) { Files.delete(Paths.get(dropSQLFile)); } export.execute(targetTypes, Action.DROP, metadata); System.out.println("Done!"); }
From source file:com.premiumminds.persistence.utils.HibernateEnversDDL.java
License:Open Source License
private static void createCommand(String[] args) { String unitName;/*from www.j a v a 2 s. c om*/ String filename = null; if (args.length < 2) System.out.println("Expected unitName"); else { unitName = args[1]; if (args.length > 2) filename = args[2]; EnversSchemaGenerator esg = new EnversSchemaGenerator(HibernateDDL.getConfiguration(unitName)); org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export(); se.setOutputFile(filename); se.setFormat(true); se.setDelimiter(";"); se.execute(false, false, false, true); } }
From source file:com.premiumminds.persistence.utils.HibernateEnversDDL.java
License:Open Source License
private static void createDropCommand(String[] args) { String unitName;/*from w w w .j a v a2s .c o m*/ String filename = null; if (args.length < 2) System.out.println("Expected unitName"); else { unitName = args[1]; if (args.length > 2) filename = args[2]; EnversSchemaGenerator esg = new EnversSchemaGenerator(HibernateDDL.getConfiguration(unitName)); org.hibernate.tool.hbm2ddl.SchemaExport se = esg.export(); se.setOutputFile(filename); se.setFormat(true); se.setDelimiter(";"); se.execute(false, false, true, true); } }
From source file:com.siemens.scr.avt.ad.util.HibernateUtil.java
License:Open Source License
public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SchemaExport schemaExport = new SchemaExport(cfg); schemaExport.setDelimiter(";"); schemaExport.setFormat(true);//from ww w .ja va2s.c o m schemaExport.setOutputFile("createSchema.sql"); schemaExport.create(true, false); }
From source file:com.socialsite.scripts.SchemaCreator.java
License:Open Source License
public static void create() { final Configuration cfg = new Configuration().configure(); final SchemaExport schemaExport = new SchemaExport(cfg); schemaExport.setDelimiter(";"); schemaExport.setFormat(true);//from w ww . ja v a 2 s. co m final File f = new File("src/main/scripts"); f.mkdirs(); schemaExport.setOutputFile("src/main/scripts/schema.sql"); schemaExport.drop(true, true); schemaExport.create(true, true); }
From source file:com.ttech.cordovabuild.infrastructure.DDLExport.java
License:Apache License
@Test public void exportDDL() { Formatter formatter = FormatStyle.DDL.getFormatter(); Ejb3Configuration jpaConfiguration = new Ejb3Configuration().configure("cordova", null); Configuration hibernateConfiguration = jpaConfiguration.getHibernateConfiguration(); SchemaExport export = new SchemaExport(hibernateConfiguration); export.setOutputFile("my-schema.sql"); export.setDelimiter(";"); export.execute(true, false, false, true); }
From source file:com.userweave.config.MPSchemaExport.java
License:Open Source License
/** * @param args// w w w. ja v a2 s . com */ public static void main(String[] args) { // if (args.length != 1) { // System.out.println("please specify output filename"); // } // String filename = "schema.ddl";//args[0]; final AnnotationConfiguration configuration = new AnnotationConfiguration(); final Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); configuration.setProperties(properties); SchemaExport export = new SchemaExport(configuration); export.setDelimiter(";"); export.setFormat(true); File outputFile = new File(filename); export.setOutputFile(outputFile.toString()); export.execute(false, false, false, true); }
From source file:com.vecna.maven.hibernate.HibernateSchemaExportMojo.java
License:Apache License
/** * Exports the schema./*w w w . j a va 2 s. c om*/ * {@inheritDoc} */ @Override protected void executeWithMappings(Configuration configuration) throws MojoExecutionException, MojoFailureException { SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.setFormat(format); if (outputFile != null) { initializePath(); schemaExport.setOutputFile(outputFile); schemaExport.setDelimiter(delimiter); } schemaExport.execute(print, export, false, !drop); }
From source file:com.wavemaker.tools.data.ExportDB.java
License:Open Source License
@Override protected void customRun() { init();// w ww . ja va 2 s. c o m final Configuration cfg = new Configuration(); // cfg.addDirectory(this.hbmFilesDir); this.hbmFilesDir.find().files().performOperation(new ResourceOperation<com.wavemaker.tools.io.File>() { @Override public void perform(com.wavemaker.tools.io.File file) { if (file.getName().endsWith(".hbm.xml")) { cfg.addInputStream(file.getContent().asInputStream()); } } }); Properties connectionProperties = getHibernateConnectionProperties(); cfg.addProperties(connectionProperties); SchemaExport export = null; SchemaUpdate update = null; File ddlFile = null; try { if (this.overrideTable) { Callable<SchemaExport> t = new Callable<SchemaExport>() { @Override public SchemaExport call() { return new SchemaExport(cfg); } }; if (this.classesDir == null) { try { export = t.call(); } catch (Exception e) { ReflectionUtils.rethrowRuntimeException(e); } } else { export = ResourceClassLoaderUtils.runInClassLoaderContext(true, t, this.classesDir); } ddlFile = File.createTempFile("ddl", ".sql"); ddlFile.deleteOnExit(); export.setOutputFile(ddlFile.getAbsolutePath()); export.setDelimiter(";"); export.setFormat(true); String extraddl = prepareForExport(this.exportToDatabase); export.create(this.verbose, this.exportToDatabase); this.errors = CastUtils.cast(export.getExceptions()); this.errors = filterError(this.errors, connectionProperties); this.ddl = IOUtils.read(ddlFile); if (!ObjectUtils.isNullOrEmpty(extraddl)) { this.ddl = extraddl + "\n" + this.ddl; } } else { Callable<SchemaUpdate> t = new Callable<SchemaUpdate>() { @Override public SchemaUpdate call() { return new SchemaUpdate(cfg); } }; if (this.classesDir == null) { try { update = t.call(); } catch (Exception e) { ReflectionUtils.rethrowRuntimeException(e); } } else { update = ResourceClassLoaderUtils.runInClassLoaderContext(t, this.classesDir); } prepareForExport(this.exportToDatabase); Connection conn = JDBCUtils.getConnection(this.connectionUrl.toString(), this.username, this.password, this.driverClassName); Dialect dialect = Dialect.getDialect(connectionProperties); DatabaseMetadata meta = new DatabaseMetadata(conn, dialect); String[] updateSQL = cfg.generateSchemaUpdateScript(dialect, meta); update.execute(this.verbose, this.exportToDatabase); this.errors = CastUtils.cast(update.getExceptions()); StringBuilder sb = new StringBuilder(); for (String line : updateSQL) { sb = sb.append(line); sb = sb.append("\n"); } this.ddl = sb.toString(); } } catch (IOException ex) { throw new DataServiceRuntimeException(ex); } catch (SQLException qex) { throw new DataServiceRuntimeException(qex); } catch (RuntimeException rex) { if (rex.getCause() != null && rex.getCause().getMessage().contains(NO_SUITABLE_DRIVER) && WMAppContext.getInstance().isCloudFoundry()) { String msg = rex.getMessage() + " - " + UNKNOWN_DATABASE; throw new DataServiceRuntimeException(msg); } else { throw new DataServiceRuntimeException(rex); } } finally { try { ddlFile.delete(); } catch (Exception ignore) { } } }
From source file:com.zeroone.guestebook.domain.SchemaGenerator.java
License:Apache License
/** * Method that actually creates the file. * * @return the generated {@link File}./*from w w w. j a v a 2 s .com*/ */ public File generate(File directory) { SchemaExport export = new SchemaExport(metadata); export.setDelimiter(";"); File file = new File(directory, String.format("ddl_%s.sql", dialect.name().toLowerCase())); export.setOutputFile(file.getAbsolutePath()); export.setFormat(true); export.execute(true, false, false, false); return file; }