Wednesday, August 1, 2007

Ant taks for schema and database creation

name="schemaexport">

name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="master-classpath"/>

properties="hibernate.properties" quiet="yes" text="yes" drop="no" delimiter=";" output="schema-export.sql">

dir="src">

name="**/*.hbm.xml"/>

This script will generate the schema for the ORM files(.hbml and pojo files).

Paramters:

text: This property is to display the text in the output file.

quiet: This is to show the output schema in console.

The following tag is to create database structure in database. Here, there are some hard-coded values. Those needs to be get from property file.

name="addbaseschema" depends="">

message="Adding base schema objects..." />

driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/gangadb" userid="root" password="root" autocommit="yes" rdbms="mysql" onerror="continue" keepformat="yes" delimitertype="row" delimiter="/" output="mylog.log" print="false" showheaders="false">

src="src/schema-export.sql" />

refid="master-classpath" />




Some short notes on Hibernate tools.

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

Option Description

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:

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

Option Description

--quiet don't output the script to stdout

--text don't export the script to the database

--naming=eg.MyNamingStrategy select a NamingStrategy

--properties=hibernate.properties read database properties from a file

--config=hibernate.cfg.xml specify a .cfg.xml file

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:

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

Option Description

--naming=eg.MyNamingStrategy select a NamingStrategy

--properties=hibernate.properties read database properties from a file

--config=hibernate.cfg.xml specify a .cfg.xml file

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: