POJO – Hibernate Code generate using Hibernate Tools.

Download and place (unzip) the following in the same folder.

1. hibernate-3.2.5.ga.tar.gz
2. HibernateTools-3.2.2.Beta1.zip
3. middlegen-2.1 (Optional – middlegen is unstable, 2.2 still in CVS – No Maintenance)

Unzip all the above in the same directory and use the following ant build.xml and run “ant -v hiberateTool” – If your ant is already setup properly, then create hibernate.properties and change hibernate-reverse-engineering.xml’s schema and catalog.

Here catalog was oracle’s database name.

4. Create build.xml as following (hope your already familiar with ANT”)

____________________________________________________

<project name=”Hibernate3″ default=”jar” basedir=”.”>

<property file=”build.properties”/>

    <!– fileset definition for the lib directory –>
    <fileset id=”fs.lib” dir=”${dir.lib}”>
        <include name=”**/*.jar” />
    </fileset>
    <fileset id=”ora.lib” dir=”${oracle.driver.dir}”>
        <include name=”**/*.jar” />
    </fileset>
    <fileset id=”hibe.lib” dir=”${hib.dir}”>
        <include name=”**/*.jar” />
    </fileset>
    <path id=”path.lib”>
        <fileset refid=”fs.lib” />
  <fileset refid=”ora.lib” />
  <fileset refid=”hibe.lib” />
 </path>

<taskdef name=”hibernatetool”
         classname=”org.hibernate.tool.ant.HibernateToolTask”
         classpathref=”path.lib” />
  
<target name=”hibernateTool” description=”Generate Hibernate mapping files” >
<!–
  Configuration is not working for Code Gernation
  <hibernatetool destdir=”${build.dir}/generated”>
  <configuration
     configurationfile=”hibernate.oracle.cfg.xml”/>
   <hbmtemplate
     templateprefix=”pojo/”
     template=”pojo/Pojo.ftl”
     filepattern=”{package-name}/{class-name}.java”>
    <property key=”jdk5″ value=”true” />
    <property key=”ejb3″ value=”true” />
   </hbmtemplate>
  </hibernatetool>
–>
 <hibernatetool destdir=”${build.dir}/generated”>
   <classpath>
   <path location=”${build.dir}/classes”/>
   </classpath>
   <jdbcconfiguration propertyfile=”hibernate.properties”  revengfile=”hibernate.reveng.xml” />
  <!– hbm2ddl –>
   <hbm2dao/>  
   <hbm2java/>  
   <hbmtemplate
    filepattern=”{package-name}/I{class-name}Constants.java”
    templatepath=”${etc.dir}/customtemplates”
    template=”myconstants.vm” 
   />
    <hbm2java jdk5=”true”/>
 </hibernatetool>
 
</target>
  
<taskdef name=”hbm2java” classname=”net.sf.hibernate.tool.hbm2java.Hbm2JavaTask”>
 <classpath refid=”path.lib” />
</taskdef>

<taskdef name=”middlegen” classname=”middlegen.MiddlegenTask”>
 <classpath refid=”path.lib” />
</taskdef>

<target name=”hbm2java” description=”Generate Java Pojos from the Hibernate mapping files”>
 <hbm2java output=”${source.generated}” config=”hibernate.cfg.xml”>
  <fileset dir=”${hibernate.source}”>
   <include name=”**/*.hbm.xml” />
  </fileset>
 </hbm2java>
</target>

<!— There were a blog claims that CVS middlegen code works better than middlegne2.1 – so avoid MiddleGen->
<target name=”middlegen” description=”Generate Hibernate mapping files” unless=”middlegen.skip” >
 <middlegen appname=”Hibernate-Spring” prefsdir=”${middlegen.temp}” gui=”false” databaseurl=”${jdbc.url}” driver=”${jdbc.driver}” username=”${database.username}” password=”${database.password}” schema=”${database.schema}” catalog=”${database.catalog}”>
  <table name=”DW_SECURITIES” pktable=”FORUM_KEYS” singular=”forum” plural=”fora”/>
  <hibernate destination=”${source.generated}” package=”${package.name}” javaTypeMapper=”middlegen.plugins.hibernate.HibernateJavaTypeMapper” />
 </middlegen>
</target>

</project>

____________________________________________________

 

<?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>

 <schema-selection match-catalog=”databasenameOfOracle”  match-schema=”SchemaNameOfOracle” />

 <!– BIN$ is recycle bin tables in Oracle –>
 <table-filter match-name=”BIN$.*” exclude=”true” />

 <!– Exclude DoNotWantIt from all catalogs/schemas –>
 <table-filter match-name=”DoNotWantIt” exclude=”true” />

</hibernate-reverse-engineering>

____________________________________________________

#Hibernate.properties

## Oracle
hibernate.dialect org.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username db_user
hibernate.connection.password db_user_pwd
hibernate.connection.url jdbc:oracle:thin:@127.12.127.127:1521:db
hibernate.default_schema SCHEMANAME
hibernate.default_catalog DBNAME

____________________________________________________

Create build.properties for build.xml

dir.lib=value
hib.dir=value
oracle.driver.dir=value
build.dir=value
etc.dir=value
# MSJava, Hibernate, Middlegen
hibernate.home=value
hibernate.extensions.home=value
middlegen.home=value
middlegen.temp=value

# Source Folders/Packages
source.generated=value
hibernate.source=value
package.name=value

# Database connection
jdbc.url=value
jdbc.driver=value
database.username=value
database.password=value
database.schema=value
database.catalog=value

  1. No comments yet.
  1. No trackbacks yet.

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: