In Hibernate 4.x, I used to generate and export the schema as defined in annotated entities as follows (using Spring to find annotated entities on the class path):
Connection connection =
DriverManager.getConnection("jdbc:h2:mem:jooq-meta-extensions", "sa", "");
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// [...] adding annotated classes to Configuration here...
configuration.generateSchemaCreationScript(
Dialect.getDialect(configuration.getProperties()));
SchemaExport export = new SchemaExport(configuration, connection);
export.create(true, true);
This no longer works in Hibernate 5.0:
Configuration.generateSchemaCreationScript()
no longer exists- The
SchemaExport(configuration, connection)
constructor is now deprecated
I didn't really find any obvious references to this change in the migration guide apart from:
Quite a few methods have been removed from Configuration
What is the correct way to generate and export a database on an existing JDBC connection with Hibernate 5.0 based on a set of annotated entities? (Pure JPA-based solutions are fine, too)
(note, just removing the call to generateSchemaCreationScript()
seems to work, but I would prefer to be sure to get this right)
Thanks to the answers by Vlad and Gunnar, I've managed to find my way through the new configuration API to produce the equivalent export logic with the following. Of course, history shows that this API will break again, so make sure to choose the appropriate version:
Hibernate 5.2:
Hibernate 5.2 (without warnings):
The above will produce some nasty warnings, which can either be ignored:
... or you work around them by hacking the following
ConnectionProvider
into the settings (it shouldn't be required in my opinion)Hibernate 5.0:
Hibernate 4:
As a reminder, here's how this worked in Hibernate 4:
The new bootstrap API allows for many customizations, but assuming you don't need those, the shortest invocation would look like that, applying default values for service registries and all the settings:
Update: Providing example for applying configuration propperties
There are several ways to inject properties for connection URL, dialect etc. E.g. you could provide a file hibernate.properties or you use a service registry customized with the required settings:
In case one is using JPA 2.1+ - there is a very simple build-in possibility to generate the ddl. just set the following jpa properties and the ddl files will be created. With spring boot one could write a separate main class with those specific config options.
JPA 2.1+
Spring Boot with JPA 2.1+
schemagenerator.properties (put in resources folder):
Spring Boot SchemaGenerator:
One example of the new
SchemaExport
initialization is found in SchemaExportTask:Of course, you can customize it according to your needs.