![](/img/trans.png)
[英]Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cfg.Configuration.getClassMapping(className)
[英]Hibernate 5.x migration suggestions script
我正在嘗試轉換我的工作腳本(使用Hibernate 4.x),該腳本建議從實際數據庫進行遷移。 當我要跟蹤更改並將其存儲在FlyWay使用的sql腳本中時,這特別方便。
這是我的課:
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();
}
}
我在schemaValidator.validate();
遇到了一些問題schemaValidator.validate();
因為現在它期望使用Metadata,並且具有cfg.generateSchemaUpdateScriptList(dialect, meta)
不再存在。
您有一些好的開始提示嗎? 也許現在有了Hibernate 5x更好的方法呢?
您可以實例化org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl
並使用SchemaValidator 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.