I'm trying to convert my working (with Hibernate 4.x) script that suggests migrations from the actual database. This is convenient expecially when I want track changes and store them in sql script used from FlyWay.
This is my class:
public class SchemaGenerator {
private static Configuration cfg;
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/test?tinyInt1isBit=false";
private static String username = "root";
private static String password = "password";
public SchemaGenerator(String packageName) throws Exception {
cfg = new Configuration();
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
cfg.setProperty("hibernate.connection.driver_class", driver);
cfg.setProperty("hibernate.connection.url", url);
cfg.setProperty("hibernate.connection.username", username);
cfg.setProperty("hibernate.connection.password", password);
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
Reflections reflections = new Reflections("com.test");
Set<Class<? extends Object>> allClasses = reflections.getTypesAnnotatedWith(Entity.class);
for (Class c : allClasses) {
cfg.addAnnotatedClass(c);
}
}
public static void validate() {
SchemaValidator schemaValidator = new SchemaValidator(cfg);
schemaValidator.validate();
}
public List<SchemaUpdateScript> createUpdateScript(DataSource dataSource, org.hibernate.dialect.Dialect dialect)
throws SQLException {
DatabaseMetadata meta = new DatabaseMetadata(dataSource.getConnection(), dialect, cfg);
List<SchemaUpdateScript> createSQL = cfg.generateSchemaUpdateScriptList(dialect, meta);
return createSQL;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
SchemaGenerator gen = new SchemaGenerator("com.test");
Driver driver = com.mysql.jdbc.Driver.class.newInstance();
DataSource dataSource = new SimpleDriverDataSource(driver, url, username, password);
List<SchemaUpdateScript> updateCommands = gen.createUpdateScript(dataSource,
new org.hibernate.dialect.MySQL5Dialect());
if (updateCommands.size() != 0)
System.out.println("" + formatSqlSuggestionScript(updateCommands));
else
System.out.println("Already up to date");
}
private static String formatSqlSuggestionScript(List<SchemaUpdateScript> sqls) {
StringBuilder builder = new StringBuilder("\n\n****update sql suggestion:***\n\n");
for (SchemaUpdateScript str : sqls) {
builder.append("\n" + str.getScript() + ";");
}
builder.append("\n\n");
return builder.toString();
}
}
I've some problems with schemaValidator.validate();
because now it expects Metadata and with cfg.generateSchemaUpdateScriptList(dialect, meta)
that doesn't exist anymore.
Do you have some good hints to start with? Or maybe is there a better approach to do this now with Hibernate 5x?
您可以实例化org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl
并使用SchemaValidator 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.