0

I'm updating a Hibernate 3 project to Hibernate 6. At some point of the code, a schema with certain mappings is created using:

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

public class SchemaGenerator {
    private void create() {
        SchemaUpdate schemaUpdate = new SchemaUpdate(getConfig());
        schemaUpdate.execute(false, true);
    }

    private Configuration getConfig() {
        if (cfg == null) {
            cfg = new Configuration();
            cfg.setProperty("hibernate.connection.driver_class", driverClassName);
            cfg.setProperty("hibernate.connection.url", url);
            cfg.setProperty("hibernate.connection.username", username);
            cfg.setProperty("hibernate.connection.password", password);
            cfg.setProperty("hibernate.dialect", hibernateDialect);
            cfg.setProperty("hibernate.jdbc.batch_size", Integer.toString(CommonConstants.JDBC_BATCH_SIZE));
            cfg.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
            cfg.setProperty("hibernate.connection.autocommit", "true");
            cfg.setProperty("hibernate.default_schema", username);
            cfg.setProperty("hibernate.hbm2ddl.auto", "create");
            cfg.configure(configFile);
        }
        return cfg;
    }
}

My XML configuration file looks like:

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="org.application.hibernate.Table"/>
    <session-factory>
<hibernate-configuration>

The problem is that the SchemaUpdate constructor no longer accepts any parameters, and I can't find how to pass the XML configuration to that object as well. You can see on Hibernate 3 hbm2ddl.SchemaUpdate docs how this was valid.

I've been able to see on the Hibernate docs that automatic schema generation is still supported, but the examples are not really explaining what I'm looking for.

Dialvive
  • 356
  • 7
  • 19
  • I think you need to read the "Bootstrap" chapter before the chapter you linked. – Mark Rotteveel Mar 22 '23 at 08:50
  • I've read thoroughly the chapter you mention @MarkRotteveel , and I see that the application is correctly bootstrapped. I think I forgot to mention the schema creation needs to happen at runtime at any requested time. – Dialvive Mar 22 '23 at 23:56
  • See [Legacy Bootstrap](https://docs.jboss.org/hibernate/orm/6.0/userguide/html_single/Hibernate_User_Guide.html#appendix-legacy-bootstrap) – Dialvive Mar 23 '23 at 00:12

1 Answers1

0

You can use a Map to build the Metadata object, which can then be passed to SchemaExport execute method:

public void createSchema() {
        Map<String, Object> settings = new HashMap<>();
        settings.put("hibernate.connection.driver_class", driverClassName);
        settings.put("hibernate.connection.url", url);
        settings.put("hibernate.connection.username", username);
        settings.put("hibernate.connection.password", password);
        settings.put("hibernate.dialect", hibernateDialect);
        settings.put("hibernate.jdbc.batch_size", Integer.toString(CommonConstants.JDBC_BATCH_SIZE));
        settings.put("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
        settings.put("hibernate.connection.autocommit", "true");
        settings.put("hibernate.default_schema", username);
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(settings)
                .configure(configFile)  // We could also add annotated classes at metadata object (?)
                .build();
        MetadataSources metadata = new MetadataSources(serviceRegistry);
        EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.execute(enumSet, SchemaExport.Action.CREATE, metadata.buildMetadata());
}

Source

Dialvive
  • 356
  • 7
  • 19