title: How do you programmatically generate a Hibernate JPA schema?
body: <p>I'd like to use the Hibernate/HBM2DDL schema generation as a starting point for managing my application's SQL schema using a tool like Liquibase or Flyway. To assist with that, I need a small utility in my project that I can run that will print out the auto-generated schema.</p>
  
  <p>With older versions or Hibernate, this was relatively simple. Something like the following would work:</p>
  
  <pre><code>EntityManagerFactory emf = null; // TODO: create your EMF the usual way.
  Class&lt;? extends Dialect&gt; hibernateDialectType = null; // TODO: e.g. HSQLDialect.class.
  
  Configuration hibernateConfig = new Configuration();
  hibernateConfig.setProperty(Environment.DIALECT, hibernateDialectType.getName());
  for (EntityType&lt;?&gt; entityType : emf.getMetamodel().getEntities()) {
          hibernateConfig.addAnnotatedClass(entityType.getJavaType());
  }
  
  SchemaExport schemaExporter = new SchemaExport(hibernateConfig);
  schemaExporter.setFormat(true);
  schemaExporter.setDelimiter(";");
  schemaExporter.create(Target.SCRIPT);
  </code></pre>
  
  <p>But as of at least Hibernate 5.2, the <code>SchemaExport</code> utility can no be built from a Hibernate <code>Configuration</code> instance.</p>
  
  <p>So how can this be done nowadays?</p>