Configuring Maven + Hibernate tools


Problem:  Database schema is available and need to generate hibernate configuration file, mapping files by reverse engineering. Use the generated hibernate mapping files to create POJO annotations and thereby use the annotated files for database persistence.

Solution:

This can be done using hibernate3-maven-plugin and using the goals as hbm2cfgxml,hbm2hbmxml and hbm2java  as shown below. As of now, i did this in two steps even though i am showing the entire pom here at one shot. (i.e comment execution for step2 while doing step1 and vice versa)

Step1: Generate the hibernate.cfg.xml and the mapping files.
Step2: Manally update the hibernate.cfg.xml with the <mapping resources=”path to above generated mapping.hbm.xml” /> files and then Generate the annotated classes

<build>
<plugins>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>hbm2cfgxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2cfgxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2cfgxml</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
</configuration>
</execution>

<execution>
<id>hbm2hbmxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2hbmxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2hbmxml</name>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
<componentProperties>
<packagename>provide.your.package.name</packagename>
</componentProperties>
</configuration>
</execution>

<execution>
<id>hbm2java</id>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
</component>
</components>
<componentProperties>
<implementation>annotationconfiguration</implementation>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
<format>true</format>
<export>false</false>
<configurationfile>target/classes/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
……………………
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Step 1 is fairly straightforward. To perform step 1, run the below command

mvn generate-resources

which  will generate configuration file (hbm2cfxml goal) in target/classes and hibernate mappings (hbm2hbmxml goal) in target/classes as well. Now you need to COPY them to your src/main/resources (in eclipse for example) and update the hibernate.cfg.xml with
the <mapping resources=”path to all the above generated mapping.hbm.xml” /> files

I faced issues while generating annotated classes in Step 2, key thing to generate annotated classes, with ejb3 annotations for entity classes (like @Entity, @Id etc) is to set the flag <ejb3>true<ejb3> and also provide <jdk5>true</jdk5> to turn on generics. Now comment step1 executions in pom.xml and  run the below command

mvn generate-sources

which will generate annotated classes by default at target\hibernate3\generated-sources. You can copy them over to your source and use them with the DAO’s for java persistence

,

  1. Leave a comment

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

%d bloggers like this: