I've recently updated Hibernate from 5.0 to 5.1 and the SchemaExport
API has changed. The migration docs mention this change, but do not explain how to use the newer API. Moreover, I have not been able to find any other supporting sample to fix the breaking change.
I stumbled upon this code diff that helped me solve the differences in the API: https://gitlab.nuiton.org/nuiton/topia/commit/0c57f073ad879a981e9fa3315f0e04669a57858b
Here's my code which exports schema for any Class marked with @Entity annotation to the output window.
static void getDDL(String packageName, String propertiesFile) throws IOException {
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.loadProperties(propertiesFile)
.build());
new Reflections(packageName)
.getTypesAnnotatedWith(Entity.class)
.forEach(metadata::addAnnotatedClass);
//STDOUT will export to output window, but other `TargetType` values are available to export to file or to the db.
EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.STDOUT);
SchemaExport export = new SchemaExport();
export.setDelimiter(";");
export.setFormat(true);
export.createOnly(targetTypes, metadata.buildMetadata());
}
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