java - Hibernate/JPA: Check generated sql before updating DB Schema (like .NET EF migrations) -


so trying learn hibernate/jpa , wondering if there similar .net's entity framework migrations can use.

i code first (class -> schema) approach, auto generated sql queries may strange (and dangerous) things database. want verify generated sql query , decide if want update database schema.

i have enabled show_sql property. run project in debug mode in order check generated query , stop / continue execution.

is there more elegant (proper?) way want?

edit: there icremental schema update feature? instance if rename field of model's class, hibernate/jpa following thing:

  • if hbm2ddl.auto=create-drop drop table , recreate (data loss).
  • if hbm2ddl.auto=update add new colunmn new name.

what want alter existing table.

yes, there schema generator class.

org.hibernate.tool.hbm2ddl.schemaexport

here's sample code on how use (note highly inspired post here)

    package com.mypackage.jpa.util;      import java.io.file;     import java.net.url;     import java.util.arraylist;     import java.util.list;      import org.hibernate.cfg.configuration;     import org.hibernate.tool.hbm2ddl.schemaexport;      public class schemagenerator {          private configuration cfg;          public static void main(string[] args) throws exception {              file f = new file(".");             string directory = f.getabsolutefile() + "/src/main/resources/ddl/generated/";              string packagename[] = { "com.mypackage.jpa", "com.mypackage.jpa.legacy", "com.mypackage.jpa.local",                     "com.mypackage.jpa.local.impl" };              schemagenerator gen = new schemagenerator(packagename);             gen.generate(dialect.mysql, directory);          }          @suppresswarnings("rawtypes")         public schemagenerator(string[] packagesname) throws exception {             cfg = new configuration();             cfg.setproperty("hibernate.hbm2ddl.auto", "create");              (string packagename : packagesname) {                 (class clazz : getclasses(packagename)) {                     cfg.addannotatedclass(clazz);                 }             }         }          @suppresswarnings("rawtypes")         private list<class> getclasses(string packagename) throws exception {             file directory = null;             try {                 classloader cld = getclassloader();                 url resource = getresource(packagename, cld);                 directory = new file(resource.getfile());             } catch (nullpointerexception ex) {                 throw new classnotfoundexception(packagename + " (" + directory + ") not appear valid package");             }             return collectclasses(packagename, directory);         }          private classloader getclassloader() throws classnotfoundexception {             classloader cld = thread.currentthread().getcontextclassloader();             if (cld == null) {                 throw new classnotfoundexception("can't class loader.");             }             return cld;         }          private url getresource(string packagename, classloader cld) throws classnotfoundexception {             string path = packagename.replace('.', '/');             url resource = cld.getresource(path);             if (resource == null) {                 throw new classnotfoundexception("no resource " + path);             }             return resource;         }          @suppresswarnings("rawtypes")         private list<class> collectclasses(string packagename, file directory) throws classnotfoundexception {             list<class> classes = new arraylist<>();             if (directory.exists()) {                 string[] files = directory.list();                 (string file : files) {                     if (file.endswith(".class")) {                         // removes .class extension                         classes.add(class.forname(packagename + '.' + file.substring(0, file.length() - 6)));                     }                 }             } else {                 throw new classnotfoundexception(packagename + " not valid package");             }             return classes;         }          private void generate(dialect dialect, string directory) {             cfg.setproperty("hibernate.dialect", dialect.getdialectclass());             schemaexport export = new schemaexport(cfg);             export.setdelimiter(";");             export.setoutputfile(directory + "ddl_" + dialect.name().tolowercase() + ".sql");             export.setformat(true);             export.execute(true, false, false, false);         }          private static enum dialect {             oracle("org.hibernate.dialect.oracle10gdialect"), mysql("org.hibernate.dialect.mysqldialect"), hsql(                     "org.hibernate.dialect.hsqldialect"), h2("org.hibernate.dialect.h2dialect");              private string dialectclass;              private dialect(string dialectclass) {                 this.dialectclass = dialectclass;             }              public string getdialectclass() {                 return dialectclass;             }         }     } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -