3 Replies Latest reply on Jul 7, 2017 2:08 AM by Shanthraj R Gowda (Shan)

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

    Gustavo Ludwig Newbie

      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:



      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,


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

          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) :















































          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:







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



            <schema-selection match-schema="MY_SCHEMA"/>



          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:




          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?
            Max Rydahl Andersen Master

            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?
              Shanthraj R Gowda (Shan) Newbie

              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.