Generating DDL Scripts from JPA Annotations with Maven

A while ago I posted an article that showed how to generate a database schema from JPA annotations. Since I didn’t get the hibernate3 maven plugin working back then I used the antrun plugin as a workaround. Thanks to the help of a reader the plugin works now, so an update is in order.

[Update 2009-06-01: The plugin is now available in version 2.2 from the central maven repository, so I removed the plugin repository configuration. I also switched from annotationconfiguration to jpaconfiguration.]

Since the last article the scenario hasn’t changed: I want to generate DDL scripts for creating a database schema from JPA annotations. The created schema can then be tweaked as necessary and used in the production environment. Like the old antrun solution, the maven plugin uses the ant task from Hibernate Tools, so the resulting schema is the same.

This is how it works. Edit your pom.xml and add the plugin configuration:

<build>
  <plugins>
    <!-- other plugins ... -->
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>hibernate3-maven-plugin</artifactId>
      <version>2.2</version>
      <executions>
        <execution>
          <phase>process-classes</phase>
          <goals>
            <goal>hbm2ddl</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <components>
          <component>
            <name>hbm2ddl</name>
            <implementation>jpaconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <persistenceunit>Default</persistenceunit>
          <outputfilename>schema.ddl</outputfilename>
          <drop>false</drop>
          <create>true</create>
          <export>false</export>
          <format>true</format>
        </componentProperties>
      </configuration>
    </plugin>
  <plugins>
</build>

Don’t forget to set the persistence unit to whatever you declared in persistence.xml.

Schema creation is executed in the process-classes phase, so you’ll find the schema in target/hibernate3/sql/schema.ddl after running mvn package (or any later phase). An alternative is to remove the <executions> element and to run the hbm2ddl goal from the command line:

  mvn hibernate3:hbm2ddl

The plugin offers a lot more than just schema creation. See the plugin documentation for more information.

Explore posts in the same categories: java

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

7 Comments on “Generating DDL Scripts from JPA Annotations with Maven”

  1. kumar Says:

    hi,
    what do you mean by “Don’t forget to set the persistence unit to whatever you declared in persistence.xml”

    To use this do I need to add mappings to all my annotated classes in hibernate.cfg.xml??
    thx

  2. mafr Says:

    In your persistence.xml you’ll find an element “persistence-unit” with an attribute “name”. The value of this attribute has to match “persistenceunit” in your maven configuration. You can set them both to “Default”, for example.

    You don’t have to add mappings to hibernate.cfg.xml. The maven plugin finds them automatically.

    Cheers,
    Matthias

  3. Bruce Says:

    Hi,
    I’m getting the following error. I don’t have a hibernate config but other than trying to generate the ddl everything works fine. Do I need this file just so that the ddl can be generated?

    Thanks

    Bruce
    [INFO] src/main/resources/hibernate.cfg.xml not found within the project. Trying absolute path.
    [INFO] No hibernate configuration file loaded.
    [INFO] src/main/resources/database.properties not found within the project. Trying absolute path.
    [INFO] No hibernate properties file loaded.
    [INFO] ————————————————————————
    [ERROR] FATAL ERROR
    [INFO] ————————————————————————
    [INFO] The dialect was not set. Set the property hibernate.dialect.
    [INFO] ————————————————————————

  4. mafr Says:

    The plugin doesn’t know for which flavor of database it should generate the DDL script. You have to supply the “hibernate.dialect” property somehow, either using a hibernate.cfg.xml file or via persistence.xml where you can also set properties for a persistence unit.

  5. Kwan Says:

    When using hibernate config (not jpaConfiguration), these following lines applied (position hibernate.cfg.xml at absolute pasth):-

    hbm2ddl

    annotationconfiguration

    annotationconfiguration

    hibernate.cfg.xml

    /Kwan

  6. Chandan Benjaram Says:

    Hi-

    I am trying your approach to generate db(MySQL) schema out of JPA Annotations in Maven way. I am getting following error during project build:
    ‘[ERROR] Persistence unit not found: ’someunit’.
    Build errors for server; org.apache.maven.lifecycle.LifecycleExecutionException: Internal error in the plugin manager executing goal ‘org.codehaus.mojo:hibernate3-maven-plugin:2.0-alpha-2:hbm2ddl’: Mojo execution failed.’

    I have my persistence unit in META-INF/persistence.xml and this is the only one XML in my whole project.

    For your information, here is my POM.xml hibernate3-maven configuration snippet:

    org.codehaus.mojo
    hibernate3-maven-plugin
    2.0-alpha-2

    hbm2ddl
    annotationconfiguration

    true
    true
    true
    jpaconfiguration
    someunit
    schema.sql
    schema.sql

    target/test-classes
    target/test-classes

    ${maven.test.skip}

    process-test-resources

    hbm2ddl

    I tried in Ant way, still no GO :-(
    I verified perstence.xml and its is getting into classpath of application.
    Do you have any idea about whats wrong with this?

    -Chandan Benjaram


Comment: