Created
June 30, 2020 09:38
-
-
Save vandeseer/04709549fbcc7de79bf7fccd8e6fb752 to your computer and use it in GitHub Desktop.
Generate DDL from Hibernate Entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hibernate.boot.MetadataSources; | |
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | |
import org.hibernate.tool.hbm2ddl.SchemaExport; | |
import org.hibernate.tool.schema.TargetType; | |
import java.util.EnumSet; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class DdlGenerator { | |
public static void main(String[] args) { | |
Map settings = new HashMap(); | |
settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); | |
MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySettings(settings).build()); | |
for (Class clazz : new Class[]{ | |
ClassOne.class, | |
ClassTwo.class | |
}) { | |
metadata.addAnnotatedClass(clazz); | |
} | |
SchemaExport schemaExport = new SchemaExport(); | |
schemaExport.createOnly(EnumSet.of(TargetType.STDOUT), metadata.buildMetadata()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment