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?
Same issue here and could not find a way to prevent it from doing so, and as a workaround I have used hsqldb: read hibernate.properties + only modified driver/url/user/password to those of hsqldb, and only then handed the properties to hibernate to process them: this way hibernate gets a database to connect to (even though it does not need one for this task), while dialect remains intact, which is important for DDL/sql-scripts generation; tested and proved to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With