我想使用Hibernate /就是hbm2ddl生成模式为起点,使用像Liquibase或迁飞工具管理我的应用程序的SQL模式。为了解决这个问题,我需要一个可以运行的小工具,它将打印出自动生成的模式。如何以编程方式生成Hibernate JPA模式?
与旧版本或休眠状态,这是比较简单的。像下面的内容将工作:
EntityManagerFactory emf = null; // TODO: create your EMF the usual way.
Class<? extends Dialect> hibernateDialectType = null; // TODO: e.g. HSQLDialect.class.
Configuration hibernateConfig = new Configuration();
hibernateConfig.setProperty(Environment.DIALECT, hibernateDialectType.getName());
for (EntityType<?> entityType : emf.getMetamodel().getEntities()) {
hibernateConfig.addAnnotatedClass(entityType.getJavaType());
}
SchemaExport schemaExporter = new SchemaExport(hibernateConfig);
schemaExporter.setFormat(true);
schemaExporter.setDelimiter(";");
schemaExporter.create(Target.SCRIPT);
但由于至少休眠5.2中,SchemaExport
工具不能从一个Hibernate Configuration
实例建成。
那么这可怎么做现在?
不知道已被添加到JPA。我会看一看! –