Howto generate Hibernate POJO and mapping files using ant from a db schema

This is the ant task I’m using to generate POJO and mapping files from a DB Schema (mysql).

<project name="springapp" basedir="." default="gen_hibernate">

	<taskdef name="hibernatetool"
			classname="org.hibernate.tool.ant.HibernateToolTask"> 
			<classpath>
				<fileset dir="lib">
					<include name="**/*.jar"/>
				</fileset>
			</classpath>
	</taskdef> 

	<target name="gen_hibernate"
			description="generate hibernate classes">
		<hibernatetool>
		
			<jdbcconfiguration
				configurationfile="hibernate.cfg.xml"
				packagename="com.openversion.bus"  
				detectmanytomany="true"
			/>
			<hbm2hbmxml destdir="src" /> 
			<hbm2java  destdir="src" />
		</hibernatetool>
	</target>

</project>

The following files should be in the lib directory added to classpath (the taskdef section in the above ant task file). Another way to use them is to copy directly in the ant lib folder.

commons-collections.jar
commons-logging.jar
dom4j-1.6.1.jar
freemarker.jar
hibernate-annotations.jar
hibernate-tools.jar
hibernate3.jar
jtidy-r8-20060801.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.5-bin.jar

If you want to generate POJO files and Hibernate .hbm.xml mapping files not for all the tables, then a new hibernate reverse engineering file has to be created specifying the table that should be used and it should be used in the ant script:

...
	<jdbcconfiguration
		configurationfile="hibernate.cfg.xml"
		packagename="com.openversion.bus"
		revengfile="tables.reveng.xml"
		detectmanytomany="true"/>
...

And here is sample tables.reveng.xml file inlcuding all the tables using a matching pattern match-name=”.*”. If you want to select only a few tables you can filter by putting match-name=”table_name” and add as many table-filter tags as many tables you want to include. You also should take care what because the tables names are case sensitive, I lost some time figuring it out.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC 
	"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
	<type-mapping>
		<sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long" not-null="true"></sql-type>
	</type-mapping>
	<table-filter match-name=".*" match-catalog="openversion"></table-filter>
</hibernate-reverse-engineering>

For further reference you can check Chapter 4. Ant Tools and if you need more details about reverse engineering file () you can check Chapter 5. Controlling reverse engineering of the Hibernate Tools Reference Guide.

Did you enjoy this tutorial? Be sure to subscribe to the my RSS feed not to miss my new posts!
... or make it popular on

9 Comments

  1. That’s very useful. But I still have a concern that is there any way so that we could merge all *.hbm.xml files to one file.

    Ex: I have some tables (tab1, tab2, …). Then, it will generate these files

    tab1.hbm.xml
    tab2.hbm.xml

    I want it should be generated to one file called mydb.hbm.xml which including all classes mapping (tab1, tab2, …)

    Any ideas is very welcome. Thanks!

Leave a Comment.