I tried using this class:
Hibernate/JPA: Check generated sql before updating DB Schema (like .NET EF migrations)
I have the following code:
pack
The solution of felix don't work anymore on hibernate 5.2 here is a version which is compatible
private void generate(Class dialect, String directory, String... packagesName) throws Exception {
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", dialect.getName())
.build());
for (String packageName : packagesName) {
LOG.info("packageName: " + packageName);
for (Class clazz : getClasses(packageName)) {
LOG.info("Class: " + clazz);
metadata.addAnnotatedClass(clazz);
}
}
MetadataImplementor metadataImplementor = (MetadataImplementor) metadata.buildMetadata();
SchemaExport export = new SchemaExport();
export.setDelimiter(";");
String filename = directory + "ddl_" + dialect.getSimpleName().toLowerCase() + ".sql";
export.setOutputFile(filename);
export.setFormat(true);
//can change the output here
EnumSet enumSet = EnumSet.of(TargetType.STDOUT);
export.execute(enumSet, SchemaExport.Action.CREATE, metadataImplementor);
}