[英]Hibernate 5.x migration suggestions script
I'm trying to convert my working (with Hibernate 4.x) script that suggests migrations from the actual database. 我正在尝试转换我的工作脚本(使用Hibernate 4.x),该脚本建议从实际数据库进行迁移。 This is convenient expecially when I want track changes and store them in sql script used from FlyWay.
当我要跟踪更改并将其存储在FlyWay使用的sql脚本中时,这特别方便。
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();
我在
schemaValidator.validate();
遇到了一些问题schemaValidator.validate();
because now it expects Metadata and with cfg.generateSchemaUpdateScriptList(dialect, meta)
that doesn't exist anymore. 因为现在它期望使用Metadata,并且具有
cfg.generateSchemaUpdateScriptList(dialect, meta)
不再存在。
Do you have some good hints to start with? 您有一些好的开始提示吗? Or maybe is there a better approach to do this now with Hibernate 5x?
也许现在有了Hibernate 5x更好的方法呢?
您可以实例化org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl
并使用SchemaValidator 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.