我正在尝试编写一个基于Hibernate提供的SchemaUpdate类的Liquibase changeset生成器。目标是利用Hibernate更新脚本的优点,并使开发人员可以根据需要进行调整。完成后,SQL将集成在Liquibase SQL文件中。
一开始这似乎非常简单,只需实例化一个SchemaUpdate类并启动execute方法即可。
但是代码什么也没做。我正在尝试从JPA persistence.xml文件初始化Hibernate,我认为这就是问题所在。
目前,我已经手动设置了mysql属性,但Hibernate找不到实体类。
从JPA配置中获取配置(由SchemaUpdate类需要)的最佳方法是什么?
我已经搜索了很长时间的解决方案,但它们都使用Hibernate 4.3中不再存在的EJB3Configuration类。
谢谢帮助!
例如,我正在做类似这样的事情:
一开始这似乎非常简单,只需实例化一个SchemaUpdate类并启动execute方法即可。
但是代码什么也没做。我正在尝试从JPA persistence.xml文件初始化Hibernate,我认为这就是问题所在。
目前,我已经手动设置了mysql属性,但Hibernate找不到实体类。
从JPA配置中获取配置(由SchemaUpdate类需要)的最佳方法是什么?
我已经搜索了很长时间的解决方案,但它们都使用Hibernate 4.3中不再存在的EJB3Configuration类。
谢谢帮助!
例如,我正在做类似这样的事情:
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);