In hibernate 4 I used config.generateSchemaCreationScript(dialect); to generate sql creation script files from our classes. After update to hibernate 5 (5.6.5.Final to be precise) I'm trying to create the same behavior.
Tried the following:
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);
This code gives me lots of warnings "java.sql.SQLException: No suitable driver" and "java.sql.SQLException: Connections could not be acquired from the underlying database!". It seems that creation of the metadata object already tries to create a database connection. But I don't have/want a database connection, I only want to create sql files from the database objects.
I've also tried to do schemaExport.execute(EnumSet.of(TargetType.SCRIPT), Action.CREATE, null, serviceRegistry);. This will still try to setup a database connection and fails on a nullpointer exception for metadata.
How can I create sql files from my Configuration in hibernate 5 without a database connection?