Generating “CREATE TABLE” SQL from a JPA / Hibernate Annotated POJO

Here’s an easy snippet of code to perform this task. It output’s the “CREATE TABLE” adjust for the desired SQL dialect

	public void entityToSQL() throws Exception{
		 Configuration cfg = new Configuration();
		  cfg.addAnnotatedClass(MyPOJO.class);
		  String[] lines = cfg.generateSchemaCreationScript(
new SQLServer2008Dialect());

		  for (String line : lines) {
			  System.out.println(line);
		  }
	}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s