3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Building on R C's answer, I made the following approach work with Hibernate 5.6:

private ServiceRegistry buildServiceRegistry() {
    var properties = new Properties();
    properties.setProperty(AvailableSettings.DIALECT, MariaDBDialect.class);
    properties.setProperty(AvailableSettings.DRIVER, org.hsqldb.jdbcDriver.class.getName());
    properties.setProperty(AvailableSettings.URL, "jdbc:hsqldb:mem:test");
    properties.setProperty(AvailableSettings.USER, "sa");
    properties.setProperty(AvailableSettings.PASS, "");

    return new StandardServiceRegistryBuilder().applySettings(properties).build();
}

The rest works as described in the question:

ServiceRegistry serviceRegistry = buildServiceRegistry();
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();

SchemaExport schemaExport = new SchemaExport();
schemaExport.setHaltOnError(true);
schemaExport.setFormat(true);
schemaExport.setDelimiter(";");
schemaExport.setOutputFile("create.sql");
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata);

Make sure to include HSQLDB in your pom.xml if you're using Maven:

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>${hsqldb.version}</version>
</dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.