Create a database using hbm2ddl and Maven

I’ve already succeeded to create classes and mapping from tables of a DB, but creating a new one from the annotated classes themselves can be very useful for a freshly created project 🙂

As a Maven addicted, I’ve used the hibernate3-maven-plugin to generate such a DB (and this way, avoiding the Ant scripting …).

In the pom.xml of your project you will have to add the following configuration of the plugin :

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>hibernate3-maven-plugin</artifactId>
 <version>2.2</version>
 <configuration>
  <components>
   <component>
    <name>hbm2ddl</name>
    <implementation>annotationconfiguration</implementation>
   </component>
  </components>
  <componentProperties>
   <drop>true</drop>
   <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
  </componentProperties>
 </configuration>
 <dependencies>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.8</version>
  </dependency>
 </dependencies>
</plugin>

The important point here, is the annotationconfiguration used. While using annotated classes, this has to be used for creating a new DB instead of a jdbcconfiguration for an existing one.The configuration file which refers to hibernate.cfg.xml is not necessary here because, by default, it refers to that file (it’s just easier for you to change something for a reason or an other ^^ ).

Choose your driver to access your DB and add it in the dependencies of the plugin (here MySQL 5.1.8).

Once all is configured, run a mvn hibernate3:hbm2ddl.

Advertisements

2 thoughts on “Create a database using hbm2ddl and Maven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s