Generate DDL with Hibernate
Last Updated on Tuesday, 20 September 2011 09:27
Generate DDL using a Java class
Hibernate core provide a Java class to generate DDL. SchemaExport class can be used to generate the schema in the Database, print schema in the console or create an sql file.
To illustrate this tutorial we will use the following annotated JPA entity :
Let's create an enumeration with the hibernate dialects we will use to generate our sql scripts. We will use MySQL,Oracle and Sybase.
Let's make the generator class. In the following class we are using SchemaExport class to do the job. Because we will not export in the database we don't need to provide a full set of configuration. We are just passing the database type as an hibernate dialect and a bunch of class. In the following example we will use only one entity class (User Entity).
The result of the execution is a file (ddl_oracle.sql) containing the following SQL to create our entity table.
DDL script for Oracle
Tags: java, tutorial, import, class, hibernate, generate, entity, schema, illustrate, javax.persistence.entity;import