in Hibernate, JPA, Maven, Oracle

Generate the database schema with Hibernate3 Maven Plugin

There is a nice Maven plugin for JPA/Hibernate that makes it possible to quickly generate the database schema (SQL) and save it in a file.
The artifactId of this plugin is hibernate3-maven-plugin.
It will scan all JPA annotations in the class files of the entities and generate the corresponding SQL queries.
A persistence.xml file is required.

  1. With version 2.2 :
  2. Content of the pom.xml :

                        <format>true</format>                    <outputfilename>schema-${DataBaseUser}-${DatabaseName}.sql</outputfilename>

    It is important to set the export property to false, so that the plugin does not drop and recreate the database.
    Here is the content of the file:


    Use the following goal :

    compile hibernate3:hbm2ddl

    The file will be created in the target folder. More exactly in /target/hibernate3/sql

  3. With version 3.0-SNAPSHOT

  4. There is a more recent version of the plugin (3.0-SNAPSHOT) but it is a SNAPSHOT version, so it is not recommended to use it in projects for various reasons (for instance it would be a problem to generate releases).
    Anyways, here is the configuration that worked for me, for this version of the plugin :

    				         <path location="${}/classes" />
    				         <path location="${project.basedir}/src/main/resources/META-INF/" />				         				         
    				      <jpaconfiguration persistenceunit="myPU" 
    				         propertyfile="src/main/resources/"  />				      
    				      <hbm2ddl create="true" export="false" drop="true" outputfilename="schema1.sql"
    				         format="true" console="true"/>

This version does not need the presence of the database driver as a direct dependency of the plugin.
The file will be created in the target folder. More exactly in /target/sql/hibernate3 (not the same folder hierarchy as with the version 2.2 of the plugin).
Links :