Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export schema after upgrading Hibernate to 5.1.0?

Tags:

java

hibernate

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.

like image 660
leojh Avatar asked Sep 24 '22 02:09

leojh


1 Answers

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());
}
like image 187
leojh Avatar answered Oct 03 '22 15:10

leojh