Automatic schema generation

Automatic schema generation
DDL may be generated from your mapping files by a Hibernate utility. The generated schema includes referential integrity constraints (primary and foreign keys) for entity and collection tables. Tables and sequences are also created for mapped identifier generators.

You must specify a SQL Dialect via the hibernate.dialect property when using this tool, as DDL is highly vendor specific.

First, customize your mapping files to improve the generated schema.

Customizing the schema
Many Hibernate mapping elements define optional attributes named length, precision and scale. You may set the length, precision and scale of a column with this attribute.

  Some tags also accept a not-null attribute (for generating a NOT NULL constraint on table columns) and a unique attribute (for generating UNIQUE constraint on table columns).

  A unique-key attribute may be used to group columns in a single unique key constraint. Currently, the specified value of the unique-key attribute is not used to name the constraint in the generated DDL, only to group the columns in the mapping file.

  An index attribute specifies the name of an index that will be created using the mapped column or columns. Multiple columns may be grouped into the same index, simply by specifying the same index name.

  A foreign-key attribute may be used to override the name of any generated foreign key constraint.

 Many mapping elements also accept a child element. This is particularly useful for mapping multi-column types:

   The default attribute lets you specify a default value for a column (you should assign the same value to the mapped property before saving a new instance of the mapped class).

    The sql-type attribute allows the user to override the default mapping of a Hibernate type to SQL datatype.

<property name="balance" type="float"> <column name="balance" sql-type="decimal(13,3)"/> The check attribute allows you to specify a check constraint.

<property name="foo" type="integer"> <column name="foo" check="foo > 10"/> <class name="Foo" table="foos" check="bar < 100.0"> ...    <property name="bar" type="float"/> Table 20.1. Summary

The element allows you to specify comments for the generated schema.

<class name="Customer" table="CurCust"> Current customers only ...        Balance in USD This results in a comment on table or comment on column statement in the generated DDL (where supported).

Running the tool
The SchemaExport tool writes a DDL script to standard out and/or executes the DDL statements.

java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files

SchemaExport Command Line Options
You may even embed SchemaExport in your application:

Configuration cfg = ....; new SchemaExport(cfg).create(false, true);

Properties
Database properties may be specified

as system properties with -D

in hibernate.properties

in a named properties file with --properties

The needed properties are:

SchemaExport Connection Properties
Property Name Description hibernate.connection.driver_class jdbc driver class hibernate.connection.url jdbc url hibernate.connection.username database user hibernate.connection.password user password hibernate.dialect dialect

Using Ant
You can call SchemaExport from your Ant build script:

<taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="class.path"/> <schemaexport properties="hibernate.properties" quiet="no" text="no" drop="no" delimiter=";" output="schema-export.sql"> <include name="**/*.hbm.xml"/>

Incremental schema updates
The SchemaUpdate tool will update an existing schema with "incremental" changes. Note that SchemaUpdate depends heavily upon the JDBC metadata API, so it will not work with all JDBC drivers.

java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files

SchemaUpdate Command Line Options
You may embed SchemaUpdate in your application:

Configuration cfg = ....; new SchemaUpdate(cfg).execute(false);

Using Ant for incremental schema updates
You can call SchemaUpdate from the Ant script:

<taskdef name="schemaupdate" classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask" classpathref="class.path"/> <schemaupdate properties="hibernate.properties" quiet="no"> <include name="**/*.hbm.xml"/>

Schema validation
The SchemaValidator tool will validate that the existing database schema "matches" your mapping documents. Note that SchemaValidator depends heavily upon the JDBC metadata API, so it will not work with all JDBC drivers. This tool is extremely useful for testing.

java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaValidator options mapping_files

SchemaValidator Command Line Options
You may embed SchemaValidator in your application:

Configuration cfg = ....; new SchemaValidator(cfg).validate;

Using Ant for schema validation
You can call SchemaValidator from the Ant script:

<target name="schemavalidate"> <taskdef name="schemavalidator" classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask" classpathref="class.path"/> <schemavalidator properties="hibernate.properties"> <include name="**/*.hbm.xml"/>