technologist, entrepreneur, vision­ary, programmer

How to use Hibernate to generate a DDL script from your Play! Framework project


ddl scriptOk, so you have been using the hibernate property name=”hibernate.hbm2ddl.auto” value=”update” to continuously update your database schema, but now you need a complete DDL script?

Use this method from you Global Class onStart to export the DDL scripts.  Just give it the package name (with path) of your Entities as well as a file name:


    public void onStart(Application app) {
        exportDatabaseSchema("models", "create_tables.sql");

    public void exportDatabaseSchema(String packageName, String scriptFilename) {

        final Configuration configuration = new Configuration();
        final Reflections reflections = new Reflections(packageName);
        final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
        // iterate all Entity classes in the package indicated by the name
        for (final Class<?> clazz : classes) {
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");

        SchemaExport schema = new SchemaExport(configuration);
        schema.execute(Target.SCRIPT, SchemaExport.Type.CREATE );  // just export the create statements in the script

That is it!

Thanks to @MonCalamari for answering my Question on Stackoverflow here.

If you have read this far, you may as well follow me on Twitter:

Author: poornerd

Tech­nol­o­gist, Entre­pre­neur, Vision­ary, Pro­gram­mer :: Grad­u­ated from USC (Uni­ver­sity of South­ern Cal­i­for­nia) with a degree in Com­puter Sci­ence. After 10+ years of free­lance con­sult­ing and pro­gram­ming, he co-founded Site­Force AG eBusi­ness Solu­tions in 1999 in Munich (München), Ger­many.

Leave a Reply

Required fields are marked *.