views:

26

answers:

2

I have a bunch of classes annotated with hibernate annotations. I'm using Maven, Hibernate and Spring. How can I generated the DB schema using hibernate3-maven-plugin's hbm2ddl?

+1  A: 

Hi, a short example like this:

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <components>
        <component>
          <name>hbm2ddl</name>
          <implementation>jdbcconfiguration</implementation>
        </component>
      </components>
      <componentProperties>
        <drop>true</drop>
        <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
      </componentProperties>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>jdbc.artifact.groupid</groupId>
        <artifactId>jdbc-driver</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

and of course reading the documentation would have helped.

khmarbaise
jdbcconfiguration won't work with annotations.
Pascal Thivent
Of course not. Sorry...just replace it with: annotationconfiguration
khmarbaise
A: 

Let's assume your project has the following structure:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── stackoverflow
    │   │           └── Foo.java
    │   └── resources
    │       └── META-INF
    │           └── persistence.xml
    └── test
        └── java

That the persistence.xml contains the following:

<persistence>
  <persistence-unit name="MyPu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.stackoverflow.Foo</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
      <property name="hibernate.connection.url" value="jdbc:h2:mem:Q4029456-1.0-SNAPSHOT"/>
      <property name="hibernate.connection.user" value="APP"/>
      <property name="hibernate.connection.password" value="APP"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

Then the following configuration would export the schema as part of the build:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q4029456</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.2.144</version>
    </dependency>
    ...
  </dependencies>
  <repositories>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public&lt;/url&gt;
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <components>
            <component>
              <name>hbm2ddl</name>
              <implementation>jpaconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <drop>true</drop>
            <!-- optional, but useful for later inspection -->
            <outputfilename>schema.ddl</outputfilename>
            <persistenceunit>MyPu</persistenceunit>
          </componentProperties>
        </configuration>
        <executions>
          <execution>
            <phase>process-classes</phase>
            <goals>
              <goal>hbm2ddl</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.2.144</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

Replace the jpaconfiguration and the persitence.xml by annotationconfiguration and a src/main/resources/hibernate.cfg.xml if you are not using JPA.

Below an extract of the obtained output:

$ mvn process-classes
[INFO] Scanning for projects...
...
[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) @ Q4029456 ---
...
drop table Foo if exists;
create table Foo (id bigint generated by default as identity, name varchar(255), primary key (id));
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
Pascal Thivent