我想寫一個基於由Hibernate提供的SchemaUpdate類的Liquibase變更集生成器。 目標是獲得Hibernate更新腳本的好處以及開發人員根據需要調整它們的可能性。 完成後,SQL將被集成到Liquibase SQL文件中。休眠4.3 SchemaUpdate與JPA
通過實例化SchemaUpdate類並啓動execute方法,開始時看起來非常簡單。
但是代碼執行任何操作。我想從JPA persistence.xml文件初始化Hibernate,我認爲這是問題所在。
目前我已經手動設置mysql屬性,但Hibernate沒有找到實體類。
從JPA配置獲取配置(SchemaUpdate類所需)的最佳方式是什麼?
我已經搜索了一段時間的解決方案,但所有人都使用EJB3Configuration類,而這個類在Hibernate 4.3中不再存在。
Thaks求助!
例如,我在做這樣的事情:
final File persistenceFile = new File("src/main/resources/META-INF/persistence.xml");
final Configuration cfg = new Configuration();
if (! persistenceFile.exists()) {
throw new FileNotFoundException(persistenceFile.getAbsolutePath() + " not found");
}
final List<String> jpaConfiguration = Files.readLines(persistenceFile, Charsets.UTF_8);
for (final String line : jpaConfiguration) {
if (line.contains("property")) {
final String[] props = line.split("\\\"");
cfg.setProperty(props[1], props[3]);
}
}
final SchemaUpdate updater = new SchemaUpdate(cfg);
updater.setOutputFile("target/script.sql");
updater.execute(true, false);
我已經通過手動查找源目錄中的實體並使用addAnnotatedClass()方法解決了該問題。 – user2810476