Discussion:
[jboss-user] [JBoss Tools] - how can I reverse engineer and create POJOs from an existing database?
Gustavo Ludwig
2013-03-07 14:09:44 UTC
Permalink
Gustavo Ludwig [https://community.jboss.org/people/gtludwig] created the discussion

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

To view the discussion, visit: https://community.jboss.org/message/801327#801327

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

Reply to this message by going to Community
[https://community.jboss.org/message/801327#801327]

Start a new discussion in JBoss Tools at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2128]
John Citizen
2013-03-08 04:49:46 UTC
Permalink
John Citizen [https://community.jboss.org/people/johnqcitizen] created the discussion

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

To view the discussion, visit: https://community.jboss.org/message/801478#801478

--------------------------------------------------------------
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 POMN 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).

You will also need to define a src/main/resources/hibernate.propeties 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 <server>, <port>, etc.

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 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.xm 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.

Hope that helps.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/801478#801478]

Start a new discussion in JBoss Tools at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2128]
Max Rydahl Andersen
2013-03-08 10:16:25 UTC
Permalink
Max Rydahl Andersen [https://community.jboss.org/people/maxandersen] created the discussion

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

To view the discussion, visit: https://community.jboss.org/message/801519#801519

--------------------------------------------------------------
http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html 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.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/801519#801519]

Start a new discussion in JBoss Tools at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2128]
Loading...