Search code examples
javahibernatehibernate-tools

best way to reverse engineer pojos using hibernate


What is the best (easiest) way to reverse-engineer POJOs from a database? I would like to generate probably 40 entity classes from tables, just to save a bunch of typing. I would like to use the Hibernate Tools toolset but all examples seem incomplete or contradictory - some reference using Ant tasks, some reference Maven plugins, and the Jboss site itself indicates that Hibernate Tools 4.x now seems to be an Eclipse plugin!

What is the "correct" way to do this, starting from scratch?


Solution

  • I actually wound up using an Ant task. If you have a situation where you need to reverse-engineer POJOs from a database, and you have no existing infrastructure in place, I believe the Ant method is best. I started with this excellent blog post and was able to cut and paste most of the code I needed. I found through experimentation that some additional JARs were needed and after some tweaking was able to generate the POJOs I needed in fairly short order.

    This assumes that you know basic Java terminology and a little about Ant, and have both installed. Here are the steps.

    You'll need to create two files (build.xml and hibernate.cfg.xml) and download some JARs. You may also need to download the Hibernate DTD files if you are behind a proxy or firewall (since Hibernate will try to go out and read the DTDs). That's it.

    Create the following directories:

    /myantproject
        /lib
        /src
    

    In your "myantproject" directory create your build.xml file as follows:

    <project name="antbuild" 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.mycompany.model"
                        detectmanytomany="true"
                        />
                <hbm2hbmxml destdir="src" />
                <hbm2java  destdir="src" />
            </hibernatetool>
        </target>
    
    </project>
    

    Also in the "myantproject" directory create your hibernate.cfg.xml file as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"    >
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.ibm.as400.access.AS400JDBCDriver</property>
            <property name="hibernate.connection.url">jdbc:as400://myserver;libraries=MYLIB;dateformat=iso;timeformat=iso;prompt=false;naming=system;transaction isolation=none</property>
            <property name="hibernate.connection.username">myuser</property>
            <property name="hibernate.connection.password">mypassword</property>
            <property name="hibernate.dialect">org.hibernate.dialect.DB2400Dialect</property>
        </session-factory>
    </hibernate-configuration>
    

    If you are behind a firewall/proxy, you can download the DTD change the DTD reference in the file to this (make sure you edit it to point to your actual file location):

    "file:///mypath/myantproject/lib/hibernate-configuration-3.0.dtd"
    

    You can then download the DTD from the original URL and stick it in your "lib" directory.

    Here are the JARs I wound up with. With these JARs, you should be able to run this Ant task and it will reverse-engineer all the tables in the database you have pointed to in "hibernate.cfg.xml".

    cglib-nodep-2.2.3.jar
    commons-collections-3.2.1.jar
    commons-logging-1.1.1.jar
    dom4j-1.6.1.jar
    freemarker-2.3.8.jar
    hibernate-annotations-3.5.0-Final.jar
    hibernate-commons-annotations-4.0.1.Final.jar
    hibernate-configuration-3.0.dtd
    hibernate-core-3.3.1.GA.jar
    hibernate-entitymanager-4.2.0.Final.jar
    hibernate-tools-3.2.3.GA.jar
    jt400-6.6.jar
    jtidy-r938.jar
    log4j-1.2.16.jar
    slf4j-api-1.7.5.jar
    

    These come from various sources - most either from apache.org or hibernate.org. You will need your database JDBC JAR from your database vendor (in this case an AS400 connector jar from IBM) to connect to the database. I also needed to download these DTDs since I was behind a firewall:

    hibernate-mapping-3.0.dtd
    hibernate-reverse-engineering-3.0.dtd
    

    Good luck!