<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FactoryPattern.com &#187; Hibernate</title>
	<atom:link href="http://www.factorypattern.com/category/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:08:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Howto generate Hibernate POJO and mapping files using ant from a db schema</title>
		<link>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/</link>
		<comments>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 18:18:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/</guid>
		<description><![CDATA[This is the ant task I&#8217;m using to generate POJO and mapping files from a DB Schema (mysql).
&#60;project name="springapp" basedir="." default="gen_hibernate"&#62;

	&#60;taskdef name="hibernatetool"
			classname="org.hibernate.tool.ant.HibernateToolTask"&#62; 
			&#60;classpath&#62;
				&#60;fileset dir="lib"&#62;
					&#60;include name="**/*.jar"/&#62;
				&#60;/fileset&#62;
			&#60;/classpath&#62;
	&#60;/taskdef&#62; 

	&#60;target name="gen_hibernate"
			description="generate hibernate classes"&#62;
		&#60;hibernatetool&#62;
		
			&#60;jdbcconfiguration
				configurationfile="hibernate.cfg.xml"
				packagename="com.openversion.bus"  
				detectmanytomany="true"
			/&#62;
			&#60;hbm2hbmxml destdir="src" /&#62; 
			&#60;hbm2java  destdir="src" /&#62;
		&#60;/hibernatetool&#62;
	&#60;/target&#62;

&#60;/project&#62;
The following files should be in the lib directory added to classpath (the taskdef section in the above ant [...]]]></description>
			<content:encoded><![CDATA[<p>This is the ant task I&#8217;m using to generate POJO and mapping files from a DB Schema (mysql).<span id="more-5"></span></p>
<pre><code>&lt;project name="springapp" basedir="." default="gen_hibernate"&gt;

	&lt;taskdef name="hibernatetool"
			classname="org.hibernate.tool.ant.HibernateToolTask"&gt; 
			&lt;classpath&gt;
				&lt;fileset dir="lib"&gt;
					&lt;include name="**/*.jar"/&gt;
				&lt;/fileset&gt;
			&lt;/classpath&gt;
	&lt;/taskdef&gt; 

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

&lt;/project&gt;</code></pre>
<p>The following files should be in the lib directory added to classpath (the <strong>taskdef</strong> section in the above ant task file). Another way to use them is to copy directly in the ant lib folder.</p>
<pre><code>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</code></pre>
<p>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:</p>
<pre><code>...
	&lt;jdbcconfiguration
		configurationfile="hibernate.cfg.xml"
		packagename="com.openversion.bus"
		revengfile="tables.reveng.xml"
		detectmanytomany="true"/&gt;
...</code></pre>
<p>And here is sample tables.reveng.xml file inlcuding all the tables using a matching pattern <strong>match-name=&#8221;.*&#8221;</strong>. If you want to select only a few tables you can filter by putting <strong>match-name=&#8221;table_name&#8221;</strong> and add as many <strong>table-filter</strong> 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.</p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE hibernate-reverse-engineering PUBLIC 
	"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" &gt;

&lt;hibernate-reverse-engineering&gt;
	&lt;type-mapping&gt;
		&lt;sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long" not-null="true"&gt;&lt;/sql-type&gt;
	&lt;/type-mapping&gt;
	&lt;table-filter match-name=".*" match-catalog="openversion"&gt;&lt;/table-filter&gt;
&lt;/hibernate-reverse-engineering&gt;</code></pre>
<p>For further reference you can check <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html">Chapter 4. Ant Tools</a> and if you need more details about reverse engineering file () you can check <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/reverseengineering.html">Chapter 5. Controlling reverse engineering</a> of the <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/index.html">Hibernate Tools Reference Guide</a>.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li><li><a href="http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" title="How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar">How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</a></li><li><a href="http://www.factorypattern.com/how-to-create-jar-files-from-ant/" title="How to Create Jar Files from Ant">How to Create Jar Files from Ant</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.530 seconds -->
