3 Replies Latest reply on Jul 7, 2017 2:08 AM by shanthraj92

    how can I reverse engineer and create POJOs from an existing database?

    gtludwig

      I need, as a prototype evaluation, to develop a new version of an existing application which consults an existing database that uses the MyISAM engine, hence no foreign keys. So, I hoped to create the POJOs from the database and build the new app from the bottom. Since this application can not write data into the database, HIbernate would ease querying data via HQL.

       

      I found this wikihow on the subject:

      http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables

       

      But it generated one pojo and one pojoId class for each entity. I don't know if this is right or not, but afterwards I got tangle up on my other chores and haven't been able to continue on it.

       

      Can someone throw me some pointers and hopefully a good tutorial on the matter?

       

      Thanks in advance,

      gtludwig

        • 1. Re: how can I reverse engineer and create POJOs from an existing database?
          johnqcitizen

          You don't have to use eclipse plugins here. You can also use the hibernate plugin for maven, and generate mapping files and POJOs from the command line.

           

          You would define a pom.xml file as follows (I've left out some of the normal POM stuff here) :

           

          <project>

            <build>

              <plugins>

               <plugin>

                 <groupId>org.codehaus.mojo</groupId>

                 <artifactId>hibernate3-maven-plugin</artifactId>

                 <version>2.2</version>

                   <configuration>

                     <components>

                       <component>

                         <name>hbm2hbmxml</name>

                         <implementation>jdbcconfiguration</implementation>

                         <outputDirectory>target/generated-resources/hibernate3</outputDirectory>

                       </component>

                       <component>

                         <name>hbm2java</name>

                         <implementation>jdbcconfiguration</implementation>

                         <outputDirectory>target/generated-sources/hibernate3</outputDirectory>

                       </component>

                     </components>

                     <componentProperties>

                       <revengfile>drc/main/resources/reveng.xml</revengfile>

                       <propertyfile>src/main/resources/hibernate.properties</propertyfile>

                       <packagename>com.whatever.domain</packagename>

                       <jdk5>true</jdk5>

                       <ejb3>true</ejb3>

                     </componentProperties>

                   </configuration>

                   <dependencies>

                     <dependency>

                       <groupId>cglib</groupId>

                       <artifactId>cglib-nodep</artifactId>

                       <version>2.2.2</version>

                     </dependency>

                     <dependency>

                       <groupId>com.oracle</groupId>

                       <artifactId>ojdbc</artifactId>

                       <version>11.1.0.6.0</version>

                     </dependency>

                   </dependencies>

                 </plugin>

              </plugin>

            </build>

          </project>

           

          You will need to change the values for the JDBC dependency (I'm using oracle), and the package name, etc.

           

          You will also need to define a src/main/resources/hibernate.properties file:

           

          hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver

          hibernate.connection.url=jdbc:oracle:thin:@<server>:<port>:<instance>

          hibernate.connection.username=<username>

          hibernate.connection.password=<password>

           

          using suitable values for driver_class, url, username, password.

           

          And a src/main/resources/reveng.xml file to define your reverse engineering strategy, e.g.

           

          <?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-schema="MY_SCHEMA"/>

          </hibernate-reverse-engineering>

           

          This specifies that only tables in the MY_SCHEMA schema are used for generating POJOs.

           

          Then run the following from the command line to generate POJOs with annotations:

           

          $ mvn hibernate3:hbm2java

           

          If you want to generate mapping files and POJOs without annotations, change the <ejb3> tag in the pom.xml file to the following:

           

          <ejb3>false</ejb3>

           

          then run the following:

           

          $ mvn hibernate3:hbm2hbmxml

          $ mvn hibernate3:hbm2java

           

          This is just a taster. You will need to look up additional information to get this working. It mostly works for me (I am having one issue with an oracle schema).

           

          Hope that helps.

          1 of 1 people found this helpful
          • 2. Re: how can I reverse engineer and create POJOs from an existing database?
            maxandersen

            http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html is a good start

             

            If your database does not have foreign key assocations there are no info available to make assocations. That is why you need a reveng.xml to describe these assocations.

            • 3. Re: how can I reverse engineer and create POJOs from an existing database?
              shanthraj92

              Hello Max,

               

              I was able to generate Java POJOs for entities, but mapping (OneTo One/ManyToOne/OneToMany/ManyToMany)between the generated entities are not proper. kindly do the need full.