Maven2 hibernate DDL generator POM

Information

This is a package of files which will make it easy to add database DDL generation to any maven 2 project which is using hibernate. It was so hard to get this working that I decided it really should be documented. This will allow you to generate DDL for mysql, oracle, derby, DB2, mssql, postgres, and hsqldb by just running a simple mvn command (after some setup).

Where do I get it?

You can download the files as a zip: maven2-hibernate-ddl.zip
You can also see the files in SVN here: https://source.sakaiproject.org/contrib/programmerscafe/trunk/maven2-hibernate-ddl/
Here is an example of a project which is using it: https://source.sakaiproject.org/contrib/caret/hierarchy/branches/1.1-DDL

How do I use it?

Just follow the readme.txt file which is included with the files. It is reproduced below so you can get a sense of the work involved:

You must have the following:
1) Java and Maven 2 installed and your project setup as a maven 2 project
2) A set of hibernate HBM files and matching persistent POJOs
3) A project structure like so (where impl is the root directory in this case):
impl

  • src
    • java
      • **.hbm.xml

How to use this:
1) Copy the contents of this zip/folder into a impl/src/ddl folder (referred to as the ddl folder) so that all the files included are located inside your source code tree
NOTE: You cannot copy this into a folder which already contains a pom.xml
2) Edit the pom.xml file
2A) Change the name, groupId, and artifactId to match your project:
<name>Sakai Hierarchy DB DDL generator</name>
<groupId>org.sakaiproject.hierarchy</groupId>
<artifactId>hierarchy-ddl-generator</artifactId>
2B) Change the parent POM to refer to your project base POM, if you have no project base POM then you can remove the <parent> tag
<!-- this should refer to your project base POM -->
<parent>
<artifactId>hierarchy</artifactId>
<groupId>org.sakaiproject</groupId>
<version>1.2.0-SNAPSHOT</version><!-hierarchy.version->
</parent>
2C) Change the project.ddl.name to match your project, this will be the name used for the ddl files (example: hierarchy.sql)
<properties>
<!-- change this to reflect the name to use for the ddl files -->
<project.ddl.name>hierarchy</project.ddl.name>
</properties>
2D) Change the dependencies to pull in your hibernate persistent POJOs (these are needed in order to process the HBM files)
<!-- this should pull in your hibernate persistent POJOs -->
<dependency>
<groupId>org.sakaiproject.hierarchy</groupId>
<artifactId>hierarchy-api</artifactId>
<version>1.2.0-SNAPSHOT</version><!-hierarchy.version->
<scope>provided</scope>
</dependency>
2E) Change the build resources to pull in your .hbm.xml files
<resource>
<!-- this should pull in your .hbm.xml files -->
<directory>${basedir}/../java</directory>
<includes>
<include>*/.xml</include>
</includes>
<filtering>false</filtering>
</resource>
3) Edit the hibernate.cfg.xml file,
the mapping resources must point to all your HBM files
<session-factory>
<!-- these mapping resource paths must point to your hibernate template files (.hbm.xml),
this should be the classpath location (typically after the src directory) -->
<mapping resource="org/sakaiproject/hierarchy/dao/hbm/HierarchyNodeMetaData.hbm.xml" />
<mapping resource="org/sakaiproject/hierarchy/dao/hbm/HierarchyPersistentNode.hbm.xml" />
</session-factory>
4) Run maven 2 to generate the DDL from the ddl folder: mvn install
You should now have a new set of folders inside the ddl folder, each one will contain a ddl file for a specific database with the database name as the name of the folder containing the ddl file
Example: ddl/mysql/hierarchy.sql

That's all there is to it.