Create Schema Using Hibernate

Getting Hibernate to Create Schema Creation SQL

I’ve seen some webpages that describe the SchemaExport Ant task. But it did not work when I tried to use it. The documentation for it was sparse. In any case, I traced through the underlying Hibernate code and found out that you can generate the schema creation SQL with just three lines of code:

package com.codebits;


import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.PostgreSQLDialect;

public class Play {

 public static void main(String[] args) {
  Configuration cfg = new Configuration();
  cfg.addDirectory(new File("config"));
  String[] lines = cfg.generateSchemaCreationScript(new PostgreSQLDialect());

  for (int i = 0; i < lines.length; i++) {
   System.out.println(lines[i] + ";");


Place your .hbm.xml files into some directory (I called mine config) and then execute the above class. Your schema creation script will be displayed on the console.




  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: