Maven Java hibernation source code generation

I am busy converting an existing project from an Ant build to one using Maven. Part of this assembly includes the use of the hibernate hbm2java tool to convert a collection of .hbm.xml files into Java. An Ant script fragment was used here:

<target name="dbcodegen" depends="cleangen" description="Generate Java source from Hibernate XML"> <hibernatetool destdir="${src.generated}"> <configuration> <fileset dir="${src.config}"> <include name="**/*.hbm.xml"/> </fileset> </configuration> <hbm2java jdk5="true"/> </hibernatetool> </target> 

I looked on the Internet and some people seem to do this (I think) using Ant inside Maven and others with the Maven plugin. I would prefer not to mix Ant and Maven. Can anyone suggest a way to do this so that all .hbm.xml files are collected and the code is generated as part of the Maven code generation phase?

Thanks!

Adam.

+6
maven-2 orm hibernate ant hbm2java
source share
3 answers

Well, there is a Maven Hibernate3 Plugin if you don't want to mix Ant and Maven (this is a good idea here IMO). It has hbm2java , which by default is bound to the generate-sources phase. See the Mojo website for more details, but the plugin setup might look something like this:

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>hbm2java</goal> </goals> </execution> </executions> <configuration> <components> <component> <name>hbm2java</name> <implementation>configuration</implementation> <outputDirectory>target/generated-sources/hibernate3</outputDirectory> </component> </components> <componentProperties> <drop>true</drop> <jdk5>true</jdk5> <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile> </componentProperties> </configuration> </plugin> 

EDIT: The plugin is actually looking for .hbm.xml in target/classes to generate java source files. So, if you put the mapping files in src/main/resources , they will be copied to target/classes during the process-resources phase, which is called by the plugin, and everything will work. I just checked this with the following example project:

  maven-hibernate3-testcase
 | - pom.xml
 `- src
     | - main
     |  | - java
     |  `- resources
     |  | - Person.hbm.xml
     |  `- hibernate.cfg.xml
     `- test
         `- java

pom.xml almost empty, it just contains the plugin configuration shown above and the junit dependency. hibernate.cfg.xml contains:

 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property> <property name="connection.username">app</property> <property name="connection.password">app</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.DerbyDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">false</property> <!-- Mapping files --> <mapping resource="Person.hbm.xml" /> </session-factory> </hibernate-configuration> 

And Person.hbm.xml looks like this:

 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Person" table="person"> <id name="id" type="int"> <generator class="increment" /> </id> <property name="name" column="cname" type="string" /> </class> </hibernate-mapping> 

In this configuration, running mvn install generates Person.java , as shown below:

 $ cat target/generated-sources/hibernate3/Person.java // default package // Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA /** * Person generated by hbm2java */ public class Person implements java.io.Serializable { private int id; private String name; public Person() { } public Person(String name) { this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } 

Everything works as described.

+13
source share

Pascal, thanks again for your help! Your solution works well.

A few other things that I encountered while working on this. The first refers to the fact that this is a fairly large project, so I split it into several Maven modules to reflect the original ant multi-directory construct. The module containing the generated classes does not actually have access to the database, therefore the hibernate.cfg.xml file is not needed and in this case should not contain information about connecting to the database. I tried this and it works fine with the cut out version of the file provided by Pascal, with all property tags removed.

With this in place, the assembly worked perfectly from the command line. However, try as I could, I could not convince other modules to collect the generated classes when starting from Eclipse. Currently, the solution I have for this is to add the following line to the POM in the configuration / components / component section:

 <outputDirectory>/src/main/java</outputDirectory> 

This causes the files to be generated in a place that the eclipse can pick up for other modules. Once this is done, you should build on the command line and then ask Eclipse to update the contents of the source directory to pick up new files. I don’t yet know a cleaner way to handle this ....

0
source share

If you need to include * .hbm.xml in phase compilation; edit your pom.xml and add the following code:

 <build> <resources> <resource> <directory>source/com/qfund/orm/</directory> <targetPath>com/qfund/orm/</targetPath> <includes> <include>*.hbm.xml</include> </includes> </resource> </resources> <testResources> <testResource> <directory>src/test/java/</directory> <includes> <include>*.xml</include> <include>*.xsd</include> <include>*.xslt</include> <include>*.properties</include> </includes> </testResource> </testResources> </build> 
0
source share

All Articles