I tried using this class:
Hibernate/JPA: Check generated sql before updating DB Schema (like .NET EF migrations)
I have the following code:
pack
Apparently the Configuration class can not be used. We must use the MetadataSources class to add the annotated classes.
private void generate(Dialect dialect, String directory, String[] packagesName) throws Exception {
MetadataSources metadata = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", dialect.getDialectClass())
.build());
for (String packageName : packagesName) {
log.info("packageName: " + packageName);
for (Class clazz : getClasses(packageName)) {
log.info("Class: " + clazz);
metadata.addAnnotatedClass(clazz);
}
}
SchemaExport export = new SchemaExport(
(MetadataImplementor) metadata.buildMetadata()
);
export.setDelimiter(";");
export.setOutputFile(directory + "ddl_" + dialect.name().toLowerCase() + ".sql");
export.setFormat(true);
export.execute(true, false, false, false);
}