在Hibernate 4中,我使用
尝试了以下方法:
config.generateSchemaCreationScript(dialect);
从我们的类生成SQL创建脚本文件。在升级到Hibernate 5(确切地说是5.6.5.Final)后,我正在尝试创建相同的行为。尝试了以下方法:
Configuration config = new Configuration();
for (Class<?> type : types) {
config.addAnnotatedClass(type);
}
ServiceRegistry serviceRegistry = config.getStandardServiceRegistryBuilder().applySetting(Environment.DIALECT, MariaDBDialect.class).build();
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
SchemaExport schemaExport = new SchemaExport();
schemaExport.setHaltOnError(true);
schemaExport.setFormat(true);
schemaExport.setDelimiter(";");
schemaExport.setOutputFile(new File("create.sql"));
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata);
这段代码给我带来了很多警告 "java.sql.SQLException: No suitable driver" 和 "java.sql.SQLException: Connections could not be acquired from the underlying database!"。看起来创建元数据对象时已经尝试创建数据库连接。但是我没有/不想要数据库连接,我只想从数据库对象创建SQL文件。
我也尝试过执行 schemaExport.execute(EnumSet.of(TargetType.SCRIPT), Action.CREATE, null, serviceRegistry);
。这仍然会尝试设置数据库连接,并在元数据的空指针异常上失败。
如何在Hibernate 5中从我的配置中创建SQL文件而不需要数据库连接?